forked from microsoft/autogen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[.Net] Add an example to show how to connect to third party OpenAI AP…
…I endpoint + upgrade Azure.AI.OpenAI package (microsoft#2619) * update * update * add blog
- Loading branch information
1 parent
3dbf4d7
commit 3ad1060
Showing
9 changed files
with
151 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
dotnet/sample/AutoGen.BasicSamples/Example16_OpenAIChatAgent_ConnectToThirdPartyBackend.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Example16_OpenAIChatAgent_ConnectToThirdPartyBackend.cs | ||
#region using_statement | ||
using AutoGen.Core; | ||
using AutoGen.OpenAI; | ||
using AutoGen.OpenAI.Extension; | ||
using Azure.AI.OpenAI; | ||
using Azure.Core.Pipeline; | ||
#endregion using_statement | ||
|
||
namespace AutoGen.BasicSample; | ||
|
||
#region CustomHttpClientHandler | ||
public sealed class CustomHttpClientHandler : HttpClientHandler | ||
{ | ||
private string _modelServiceUrl; | ||
|
||
public CustomHttpClientHandler(string modelServiceUrl) | ||
{ | ||
_modelServiceUrl = modelServiceUrl; | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
request.RequestUri = new Uri($"{_modelServiceUrl}{request.RequestUri.PathAndQuery}"); | ||
|
||
return base.SendAsync(request, cancellationToken); | ||
} | ||
} | ||
#endregion CustomHttpClientHandler | ||
|
||
public class Example16_OpenAIChatAgent_ConnectToThirdPartyBackend | ||
{ | ||
public static async Task RunAsync() | ||
{ | ||
#region create_agent | ||
using var client = new HttpClient(new CustomHttpClientHandler("http://localhost:11434")); | ||
var option = new OpenAIClientOptions(OpenAIClientOptions.ServiceVersion.V2024_04_01_Preview) | ||
{ | ||
Transport = new HttpClientTransport(client), | ||
}; | ||
|
||
// api-key is not required for local server | ||
// so you can use any string here | ||
var openAIClient = new OpenAIClient("api-key", option); | ||
var model = "llama3"; | ||
|
||
var agent = new OpenAIChatAgent( | ||
openAIClient: openAIClient, | ||
name: "assistant", | ||
modelName: model, | ||
systemMessage: "You are a helpful assistant designed to output JSON.", | ||
seed: 0) | ||
.RegisterMessageConnector() | ||
.RegisterPrintMessage(); | ||
#endregion create_agent | ||
|
||
#region send_message | ||
await agent.SendAsync("Can you write a piece of C# code to calculate 100th of fibonacci?"); | ||
#endregion send_message | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Program.cs | ||
|
||
await Example02_TwoAgent_MathChat.RunAsync(); | ||
using AutoGen.BasicSample; | ||
Console.ReadLine(); | ||
await Example16_OpenAIChatAgent_ConnectToThirdPartyBackend.RunAsync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
dotnet/website/articles/OpenAIChatAgent-connect-to-third-party-api.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
The following example shows how to connect to third-party OpenAI API using @AutoGen.OpenAI.OpenAIChatAgent. | ||
|
||
> [!NOTE] | ||
> You can find the complete code of this example in [Example16_OpenAIChatAgent_ConnectToThirdPartyBackend](https://github.com/microsoft/autogen/tree/dotnet/dotnet/sample/AutoGen.BasicSamples/Example16_OpenAIChatAgent_ConnectToThirdPartyBackend.cs). | ||
## Overview | ||
A lot of LLM applications/platforms support spinning up a chat server that is compatible with OpenAI API, such as LM Studio, Ollama, Mistral etc. This means that you can connect to these servers using the @AutoGen.OpenAI.OpenAIChatAgent. | ||
|
||
> [!NOTE] | ||
> Some platforms might not support all the features of OpenAI API. For example, Ollama does not support `function call` when using it's openai API according to its [document](https://github.com/ollama/ollama/blob/main/docs/openai.md#v1chatcompletions) (as of 2024/05/07). | ||
> That means some of the features of OpenAI API might not work as expected when using these platforms with the @AutoGen.OpenAI.OpenAIChatAgent. | ||
> Please refer to the platform's documentation for more information. | ||
## Prerequisites | ||
- Install the following packages: | ||
```bash | ||
dotnet add package AutoGen.OpenAI --version AUTOGEN_VERSION | ||
``` | ||
|
||
- Spin up a chat server that is compatible with OpenAI API. | ||
The following example uses Ollama as the chat server, and llama3 as the llm model. | ||
```bash | ||
ollama serve | ||
``` | ||
|
||
## Steps | ||
- Import the required namespaces: | ||
[!code-csharp[](../../sample/AutoGen.BasicSamples/Example16_OpenAIChatAgent_ConnectToThirdPartyBackend.cs?name=using_statement)] | ||
|
||
- Create a `CustomHttpClientHandler` class. | ||
|
||
The `CustomHttpClientHandler` class is used to customize the HttpClientHandler. In this example, we override the `SendAsync` method to redirect the request to local Ollama server, which is running on `http://localhost:11434`. | ||
|
||
[!code-csharp[](../../sample/AutoGen.BasicSamples/Example16_OpenAIChatAgent_ConnectToThirdPartyBackend.cs?name=CustomHttpClientHandler)] | ||
|
||
- Create an `OpenAIChatAgent` instance and connect to the third-party API. | ||
|
||
Then create an @AutoGen.OpenAI.OpenAIChatAgent instance and connect to the OpenAI API from Ollama. You can customize the transport behavior of `OpenAIClient` by passing a customized `HttpClientTransport` instance. In the customized `HttpClientTransport` instance, we pass the `CustomHttpClientHandler` we just created which redirects all openai chat requests to the local Ollama server. | ||
|
||
[!code-csharp[](../../sample/AutoGen.BasicSamples/Example16_OpenAIChatAgent_ConnectToThirdPartyBackend.cs?name=create_agent)] | ||
|
||
- Chat with the `OpenAIChatAgent`. | ||
Finally, you can start chatting with the agent. In this example, we send a coding question to the agent and get the response. | ||
|
||
[!code-csharp[](../../sample/AutoGen.BasicSamples/Example16_OpenAIChatAgent_ConnectToThirdPartyBackend.cs?name=send_message)] | ||
|
||
## Sample Output | ||
The following is the sample output of the code snippet above: | ||
|
||
![output](../images/articles/ConnectTo3PartyOpenAI/output.gif) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.