You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Chat-with-an-agent.md 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. This tutorial shows how to generate response using an @AutoGen.Core.IAgent by taking @AutoGen.OpenAI.OpenAIChatAgent as an example.
  2. > [!NOTE]
  3. > AutoGen.Net provides the following agents to connect to different LLM platforms. Generating responses using these agents is similar to the example shown below.
  4. > - @AutoGen.OpenAI.OpenAIChatAgent
  5. > - @AutoGen.SemanticKernel.SemanticKernelAgent
  6. > - @AutoGen.LMStudio.LMStudioAgent
  7. > - @AutoGen.Mistral.MistralClientAgent
  8. > - @AutoGen.Anthropic.AnthropicClientAgent
  9. > - @AutoGen.Ollama.OllamaAgent
  10. > - @AutoGen.Gemini.GeminiChatAgent
  11. > [!NOTE]
  12. > The complete code example can be found in [Chat_With_Agent.cs](https://github.com/microsoft/autogen/blob/main/dotnet/samples/AgentChat/Autogen.Basic.Sample/GettingStart/Chat_With_Agent.cs)
  13. ## Step 1: Install AutoGen
  14. First, install the AutoGen package using the following command:
  15. ```bash
  16. dotnet add package AutoGen
  17. ```
  18. ## Step 2: add Using Statements
  19. [!code-csharp[Using Statements](../../samples/AgentChat/Autogen.Basic.Sample/GettingStart/Chat_With_Agent.cs?name=Using)]
  20. ## Step 3: Create an @AutoGen.OpenAI.OpenAIChatAgent
  21. > [!NOTE]
  22. > The @AutoGen.OpenAI.Extension.OpenAIAgentExtension.RegisterMessageConnector* method registers an @AutoGen.OpenAI.OpenAIChatRequestMessageConnector middleware which converts OpenAI message types to AutoGen message types. This step is necessary when you want to use AutoGen built-in message types like @AutoGen.Core.TextMessage, @AutoGen.Core.ImageMessage, etc.
  23. > For more information, see [Built-in-messages](../articles/Built-in-messages.md)
  24. [!code-csharp[Create an OpenAIChatAgent](../../samples/AgentChat/Autogen.Basic.Sample/GettingStart/Chat_With_Agent.cs?name=Create_Agent)]
  25. ## Step 4: Generate Response
  26. To generate response, you can use one of the overloaded method of @AutoGen.Core.AgentExtension.SendAsync* method. The following code shows how to generate response with text message:
  27. [!code-csharp[Generate Response](../../samples/AgentChat/Autogen.Basic.Sample/GettingStart/Chat_With_Agent.cs?name=Chat_With_Agent)]
  28. To generate response with chat history, you can pass the chat history to the @AutoGen.Core.AgentExtension.SendAsync* method:
  29. [!code-csharp[Generate Response with Chat History](../../samples/AgentChat/Autogen.Basic.Sample/GettingStart/Chat_With_Agent.cs?name=Chat_With_History)]
  30. To streamingly generate response, use @AutoGen.Core.IStreamingAgent.GenerateStreamingReplyAsync*
  31. [!code-csharp[Generate Streaming Response](../../samples/AgentChat/Autogen.Basic.Sample/GettingStart/Chat_With_Agent.cs?name=Streaming_Chat)]
  32. ## Further Reading
  33. - [Chat with google gemini](../articles/AutoGen.Gemini/Chat-with-google-gemini.md)
  34. - [Chat with vertex gemini](../articles/AutoGen.Gemini/Chat-with-vertex-gemini.md)
  35. - [Chat with Ollama](../articles/AutoGen.Ollama/Chat-with-llama.md)
  36. - [Chat with Semantic Kernel Agent](../articles/AutoGen.SemanticKernel/SemanticKernelAgent-simple-chat.md)