如何使用Anthropic AI库

随着人工智能服务的快速发展,几乎每天都有新的服务推出。这导致很多时候只能通过REST API端点来访问服务,而没有现成的库可以使用。因此,开发自定义库来消费API端点变得非常必要。本文将介绍如何使用一个名为HigLabo.Anthropic的库来调用Claude AI服务。

为了帮助那些计划开发Claude API的开发者,创建了一个库,并在这里分享。可以通过Nuget下载这个库。所有源代码都可以在GitHub上找到。

可以通过以下链接下载HigLabo.Anthropic库:

https://github.com/higty/higlabo/tree/master/Net8

使用代码

以下是如何使用这个库来消费Claude AI服务的示例代码。

可以使用以下C#代码来接收消息流:

var cl = new AnthropicClient("API KEY"); var result = new MessagesStreamResult(); await foreach (string text in cl.MessagesStreamAsync("How to enjoy coffee?", ModelNames.Claude3Opus, result, CancellationToken.None)) { Console.Write(text); } if (result.MessageDelta != null) { Console.WriteLine("StopReason: " + result.MessageDelta.Delta.Stop_Reason); Console.WriteLine("Usage: " + result.MessageDelta.Usage.Output_Tokens); }

可以通过MessagesParameter类来传递所有API参数。

var cl = new AnthropicClient("API KEY"); var p = new MessagesParameter(); p.AddUserMessage("How to enjoy coffee?"); p.Model = ModelNames.Claude3Opus; p.Max_Tokens = 1024; // 设置其他属性... var result = new MessagesStreamResult(); await foreach (var item in cl.MessagesStreamAsync(p, result, CancellationToken.None)) { Console.Write(item); } if (result.MessageDelta != null) { Console.WriteLine("StopReason: " + result.MessageDelta.Delta.Stop_Reason); Console.WriteLine("Usage: " + result.MessageDelta.Usage.Output_Tokens); }

以下是如何调用函数的示例代码:

var cl = new AnthropicClient("API KEY"); var tools = new AnthropicTools(); var tool = new AnthropicTool("GetTickerSymbol", "Gets the stock ticker symbol for a company searched by name. Returns str: The ticker symbol for the company stock. Raises TickerNotFound: if no matching ticker symbol is found."); tool.Parameters.Add(new AnthropicToolParameter("company_name", "string", "The name of company")); tools.Add(tool); var toolXml = tool.ToString(); var p = new MessagesParameter(); p.Messages.Add(new ChatMessage(ChatMessageRole.User, $"What is the current stock price of Microsoft?")); p.SetTools(tools); p.Model = ModelNames.Claude3Opus; p.Max_Tokens = 1024; p.Stream = true; var result = new MessagesStreamResult(); await foreach (var item in cl.MessagesStreamAsync(p, result, CancellationToken.None)) { Console.Write(item); } var calls = AnthropicFunctionCalls.Parse(result.GetText()); if (calls.InvokeList.Count > 0) { Console.WriteLine(); Console.WriteLine("■Function call list"); Console.WriteLine(calls.ToString()); var invoke = calls.InvokeList.Find(el => el.ToolName == "GetTickerSymbol"); if (invoke != null) { var companyName = invoke.GetParameterValue("company_name") ?? ""; var tickerSymbol = GetTickerSymbol(companyName); } }

以下是如何发送图片并获取解释的示例代码:

var p = new MessagesParameter(); p.Model = "claude-3-opus-20240229"; p.Max_Tokens = 1024; var msg = new ChatImageMessage(ChatMessageRole.User); msg.AddTextContent($"What is this image include?"); msg.AddImageFile(Path.Combine(Environment.CurrentDirectory, "Image", "Rock.jpg")); p.Messages.Add(msg); p.Stream = true; var result = new MessagesStreamResult(); await foreach (var item in cl.MessagesStreamAsync(p, result, CancellationToken.None)) { Console.Write(item); }

注意事项

所有类设计架构都与HigLabo.OpenAI相同。可以使用与HigLabo.OpenAI相同的方式使用HigLabo.Anthropic。HigLabo.Anthropic包括以下类:

  • AnthropicClient
  • XXXParameter
  • XXXAsync
  • XXXResponse
  • RestApiResponse
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485