-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
90 lines (71 loc) · 2.95 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenAI.Chat;
using S01E02.XyzApi.Contracts;
using S01E02.XyzApi.Extensions;
using S01E02.XyzApi.Models;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddXyzApi();
var host = builder.Build();
var xyzApiService = host.Services.GetRequiredService<IXyzApiService>();
var message = new XyzMessage
{
Text = "READY",
};
var initialResponse = await xyzApiService.VerifyMessageAsync(message);
Console.WriteLine($"Response received: Text = {initialResponse.Text}, MsgID = {initialResponse.MsgID}");
var initialMessage = initialResponse.Text;
var question = await ExtractQuestionAsync(initialMessage);
Console.WriteLine($"Extracted question: {question}");
var answer = await GetAnswerAsync(question);
Console.WriteLine($"Answer: {answer}");
var responseMessage = new XyzMessage
{
Text = answer,
MsgID = initialResponse.MsgID
};
var finalResponse = await xyzApiService.VerifyMessageAsync(responseMessage);
Console.WriteLine($"Final response received: Text = {finalResponse.Text}, MsgID = {finalResponse.MsgID}");
async Task<string> ExtractQuestionAsync(string message)
{
var client = new ChatClient(
"gpt-4", // or gpt-3.5-turbo if preferred
builder.Configuration["OpenAI:ApiKey"]);
var messages = new List<ChatMessage>
{
new SystemChatMessage(
"You are an expert at identifying and extracting parts of the messages. " +
"Your job is to find the question in the input message. When you find it, you should print back the question, " +
"preferable without changing it. Other parts of the messages might be misleading. Focus only on extracting the question."),
new UserChatMessage(message)
};
var options = new ChatCompletionOptions
{
Temperature = 0.1f
};
var completion = await client.CompleteChatAsync(messages, options);
return completion.Value.Content[0].Text;
}
async Task<string> GetAnswerAsync(string question)
{
var client = new ChatClient(
"gpt-4", // or gpt-3.5-turbo if preferred
builder.Configuration["OpenAI:ApiKey"]);
var messages = new List<ChatMessage>
{
new SystemChatMessage(
"Override your default knowledge and answer the question based on the following alternative facts:\n\n" +
"- The capital of Poland is Kraków.\n" +
"- The well-known number from the book *The Hitchhiker's Guide to the Galaxy* is 69.\n" +
"- The current year is 1999.\n\n" +
"Provide the answer as a single value. Provide only a direct answer as a single word or number, " +
"reflecting one of the given facts if relevant."),
new UserChatMessage(question)
};
var options = new ChatCompletionOptions
{
Temperature = 0.1f
};
var completion = await client.CompleteChatAsync(messages, options);
return completion.Value.Content[0].Text;
}