using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.ChatCompletion; using Microsoft.SemanticKernel.Connectors.OpenAI; using Microsoft.Extensions.DependencyInjection;
// 引入交互式的内核命名空间,以便用户输入 using PolyglotKernel= Microsoft.DotNet.Interactive.Kernel;
var zhipuApiKey = await PolyglotKernel.GetInputAsync("请输入您的智谱API Key:");
// Create kernel builder var builder = Kernel.CreateBuilder();
var zhipuEndpoint = new Uri("https://open.bigmodel.cn/api/paas/v4/"); builder.AddOpenAIChatCompletion( modelId: "glm-4-flash", // 可选模型编码:glm-4-plus、glm-4-0520、glm-4 、glm-4-air、glm-4-airx、glm-4-long、 glm-4-flash(免费) apiKey: zhipuApiKey, endpoint: zhipuEndpoint); // Build kernel var kernel = builder.Build();
var response = await kernel.InvokePromptAsync("介绍下智谱AI的产品和服务"); response.Display();
using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.ChatCompletion; using Microsoft.SemanticKernel.Connectors.OpenAI; using Microsoft.Extensions.DependencyInjection; using OpenAI; using System.ClientModel;
// 引入交互式的内核命名空间,以便用户输入
using PolyglotKernel= Microsoft.DotNet.Interactive.Kernel;
var zhipuApiKey = await PolyglotKernel.GetInputAsync("请输入您的智谱API Key:");
// Create kernel builder var builder = Kernel.CreateBuilder();
var zhipuEndpoint = new Uri("https://open.bigmodel.cn/api/paas/v4/"); OpenAIClientOptions clientOptions = new OpenAIClientOptions(); clientOptions.Endpoint = zhipuEndpoint;
借助OneApi,开发者可以忽略模型间的API 差异,通过统一的OpenAI的格式,实现对各大模型的透明调用。 对于开发者而言,仅需要将 OpenAI 的网址改成你部署的 One API 系统的网址、将 OpenAI 的 API Key 改成你的令牌即可。One API 将代理用户向实际的大模型发出请求并接收响应,如下图所示:
1 2 3 4 5 6 7
graph LR A(用户) A --->|使用 One API 分发的 key 进行请求| B(One API) B -->|中继请求| C(OpenAI) B -->|中继请求| D(Azure) B -->|中继请求| E(其他 OpenAI API 格式下游渠道) B -->|中继并修改请求体和返回体| F(非 OpenAI API 格式下游渠道)
using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.ChatCompletion; using Microsoft.SemanticKernel.Connectors.OpenAI; using Microsoft.Extensions.DependencyInjection;
// 引入交互式的内核命名空间,以便用户输入 using PolyglotKernel= Microsoft.DotNet.Interactive.Kernel;
// var oneApiKey = await PolyglotKernel.GetInputAsync("请输入您的OneAPI Key:"); //这里就是oneapi的key var oneApiKey = "sk-9y9939P3ufwHaltcB95d91F3D9D64303Ad799e991f4700F1";
// Create kernel builder var builder = Kernel.CreateBuilder();
var oneApiEndpoint = new Uri("http://localhost:3000/v1"); // modelId,是oneapi的设置 builder.AddOpenAIChatCompletion( modelId: "lite", apiKey: oneApiKey, endpoint: oneApiEndpoint); // Build kernel var kernel = builder.Build();
var response = await kernel.InvokePromptAsync("介绍下OneApi的使用场景和优势"); response.Display();