|
| 1 | +using Azure; |
| 2 | +using Azure.AI.OpenAI; |
| 3 | +using Microsoft.Extensions.Configuration; |
| 4 | +using System.Text.Json; |
| 5 | + |
| 6 | +// == Retrieve the local secrets saved during the Azure deployment ========== |
| 7 | +var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build(); |
| 8 | +string openAIEndpoint = config["AZURE_OPENAI_ENDPOINT"]; |
| 9 | +string openAIDeploymentName = config["AZURE_OPENAI_GPT_NAME"]; |
| 10 | +string openAiKey = config["AZURE_OPENAI_KEY"]; |
| 11 | + |
| 12 | +// == Creating the AIClient ========== |
| 13 | +var endpoint = new Uri(openAIEndpoint); |
| 14 | +var credentials = new AzureKeyCredential(openAiKey); |
| 15 | +var openAIClient = new OpenAIClient(endpoint, credentials); |
| 16 | + |
| 17 | + |
| 18 | +// == Defining a Tool to extend the AI model ========== |
| 19 | +var getWeather = new ChatCompletionsFunctionToolDefinition() |
| 20 | +{ |
| 21 | + Name = "get_current_weather", |
| 22 | + Description = "Get the current weather in a given location", |
| 23 | + Parameters = BinaryData.FromObjectAsJson( |
| 24 | + new |
| 25 | + { |
| 26 | + Type = "object", |
| 27 | + Properties = new |
| 28 | + { |
| 29 | + Location = new |
| 30 | + { |
| 31 | + Type = "string", |
| 32 | + Description = "The city, e.g. Montreal, Sidney", |
| 33 | + }, |
| 34 | + Unit = new |
| 35 | + { |
| 36 | + Type = "string", |
| 37 | + Enum = new[] { "celsius", "fahrenheit" }, |
| 38 | + } |
| 39 | + }, |
| 40 | + Required = new[] { "location" }, |
| 41 | + }, |
| 42 | + new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }), |
| 43 | +}; |
| 44 | + |
| 45 | +// == Purely for convenience and clarity, this standalone local method handles tool call responses. ========== |
| 46 | +ChatRequestToolMessage GetToolCallResponseMessage(ChatCompletionsToolCall toolCall) |
| 47 | +{ |
| 48 | + var functionToolCall = toolCall as ChatCompletionsFunctionToolCall; |
| 49 | + if (functionToolCall?.Name == getWeather.Name) |
| 50 | + { |
| 51 | + string unvalidatedArguments = functionToolCall.Arguments; |
| 52 | + var functionResultData = (object)null; |
| 53 | + |
| 54 | + // == Here you would call a weather API to get the weather for the location ========== |
| 55 | + functionResultData = "Periods of rain or drizzle, 15 C"; |
| 56 | + |
| 57 | + return new ChatRequestToolMessage(functionResultData.ToString(), toolCall.Id); |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + throw new NotImplementedException(); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +var completionOptions = new ChatCompletionsOptions |
| 67 | +{ |
| 68 | + MaxTokens = 400, |
| 69 | + Temperature = 1f, |
| 70 | + FrequencyPenalty = 0.0f, |
| 71 | + PresencePenalty = 0.0f, |
| 72 | + NucleusSamplingFactor = 0.95f, // Top P |
| 73 | + DeploymentName = openAIDeploymentName, |
| 74 | + Tools = { getWeather } |
| 75 | +}; |
| 76 | +completionOptions.ToolChoice = ChatCompletionsToolChoice.Auto; |
| 77 | + |
| 78 | + |
| 79 | +// == Providing context for the AI model ========== |
| 80 | +var systemPrompt = |
| 81 | +""" |
| 82 | +You are a hiking enthusiast who helps people discover fun hikes in their area. You are upbeat and friendly. |
| 83 | +A good weather is important for a good hike. Only make recommendations if the weather is good or if people insist. |
| 84 | +You introduce yourself when first saying hello. When helping people out, you always ask them |
| 85 | +for this information to inform the hiking recommendation you provide: |
| 86 | +
|
| 87 | +1. Where they are located |
| 88 | +2. What hiking intensity they are looking for |
| 89 | +
|
| 90 | +You will then provide three suggestions for nearby hikes that vary in length after you get that information. |
| 91 | +You will also share an interesting fact about the local nature on the hikes when making a recommendation. |
| 92 | +"""; |
| 93 | + |
| 94 | +completionOptions.Messages.Add(new ChatRequestSystemMessage(systemPrompt)); |
| 95 | + |
| 96 | + |
| 97 | +// == Starting the conversation ========== |
| 98 | +string userGreeting = """ |
| 99 | +Hi! |
| 100 | +"""; |
| 101 | + |
| 102 | +completionOptions.Messages.Add(new ChatRequestUserMessage(userGreeting)); |
| 103 | +Console.WriteLine($"\n\nUser >>> {userGreeting}"); |
| 104 | + |
| 105 | + |
| 106 | +ChatCompletions response = await openAIClient.GetChatCompletionsAsync(completionOptions); |
| 107 | +ChatResponseMessage assistantResponse = response.Choices[0].Message; |
| 108 | +Console.WriteLine($"\n\nAssistant >>> {assistantResponse.Content}"); |
| 109 | +completionOptions.Messages.Add(new ChatRequestSystemMessage(assistantResponse.Content)); |
| 110 | + |
| 111 | + |
| 112 | +// == Providing the user's request ========== |
| 113 | +var hikeRequest = |
| 114 | +""" |
| 115 | +Is the weather is good today for a hike? |
| 116 | +If yes, I live in the greater Montreal area and would like an easy hike. I don't mind driving a bit to get there. |
| 117 | +I don't want the hike to be over 10 miles round trip. I'd consider a point-to-point hike. |
| 118 | +I want the hike to be as isolated as possible. I don't want to see many people. |
| 119 | +I would like it to be as bug free as possible. |
| 120 | +"""; |
| 121 | + |
| 122 | +Console.WriteLine($"\n\nUser >>> {hikeRequest}"); |
| 123 | +completionOptions.Messages.Add(new ChatRequestUserMessage(hikeRequest)); |
| 124 | + |
| 125 | +// == Retrieve the answer from HikeAI ========== |
| 126 | +response = await openAIClient.GetChatCompletionsAsync(completionOptions); |
| 127 | + |
| 128 | +// == If the response includes a tool call, handle it and continue the conversation ========== |
| 129 | +ChatChoice responseChoice = response.Choices[0]; |
| 130 | +if (responseChoice.FinishReason == CompletionsFinishReason.ToolCalls) |
| 131 | +{ |
| 132 | + // == Include the FunctionCall message in the conversation history ========== |
| 133 | + completionOptions.Messages.Add(new ChatRequestAssistantMessage(responseChoice.Message)); |
| 134 | + |
| 135 | + // == Add a new tool message for each tool call that is resolved ========== |
| 136 | + foreach (ChatCompletionsToolCall toolCall in responseChoice.Message.ToolCalls) |
| 137 | + { |
| 138 | + var ToolCallMsg = GetToolCallResponseMessage(toolCall); |
| 139 | + completionOptions.Messages.Add(ToolCallMsg); |
| 140 | + } |
| 141 | + |
| 142 | + // == Retrieve the answer from HikeAI Pro ========== |
| 143 | + response = await openAIClient.GetChatCompletionsAsync(completionOptions); |
| 144 | +} |
| 145 | + |
| 146 | +assistantResponse = response.Choices[0].Message; |
| 147 | +Console.WriteLine($"\n\nAssistant >>> {assistantResponse.Content}"); |
0 commit comments