2024-10-02 11:42:27 -07:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2024-07-15 12:33:10 -07:00
|
|
|
// Connect_To_Ollama.cs
|
|
|
|
|
2024-06-26 08:33:28 -07:00
|
|
|
#region using_statement
|
2024-10-15 07:23:33 -07:00
|
|
|
using System.ClientModel;
|
2024-06-26 08:33:28 -07:00
|
|
|
using AutoGen.Core;
|
2024-08-27 14:37:47 -07:00
|
|
|
using AutoGen.OpenAI.Extension;
|
|
|
|
using OpenAI;
|
2024-06-26 08:33:28 -07:00
|
|
|
#endregion using_statement
|
|
|
|
|
|
|
|
namespace AutoGen.OpenAI.Sample;
|
|
|
|
|
|
|
|
public class Connect_To_Ollama
|
|
|
|
{
|
|
|
|
public static async Task RunAsync()
|
|
|
|
{
|
|
|
|
#region create_agent
|
|
|
|
// api-key is not required for local server
|
|
|
|
// so you can use any string here
|
2024-10-15 07:23:33 -07:00
|
|
|
var openAIClient = new OpenAIClient(new ApiKeyCredential("api-key"), new OpenAIClientOptions
|
2024-08-27 14:37:47 -07:00
|
|
|
{
|
|
|
|
Endpoint = new Uri("http://localhost:11434/v1/"), // remember to add /v1/ at the end to connect to Ollama openai server
|
|
|
|
});
|
2024-06-26 08:33:28 -07:00
|
|
|
var model = "llama3";
|
|
|
|
|
|
|
|
var agent = new OpenAIChatAgent(
|
2024-08-27 14:37:47 -07:00
|
|
|
chatClient: openAIClient.GetChatClient(model),
|
2024-06-26 08:33:28 -07:00
|
|
|
name: "assistant",
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|