-
Notifications
You must be signed in to change notification settings - Fork 430
/
Copy pathChatEndpointTests.cs
144 lines (127 loc) · 4.37 KB
/
ChatEndpointTests.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using NUnit.Framework;
using OpenAI_API.Chat;
using OpenAI_API.Completions;
using OpenAI_API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace OpenAI_Tests
{
public class ChatEndpointTests
{
[SetUp]
public void Setup()
{
OpenAI_API.APIAuthentication.Default = new OpenAI_API.APIAuthentication(Environment.GetEnvironmentVariable("TEST_OPENAI_SECRET_KEY"));
}
[Test]
public void BasicCompletion()
{
var api = new OpenAI_API.OpenAIAPI();
Assert.IsNotNull(api.Chat);
var results = api.Chat.CreateChatCompletionAsync(new ChatRequest()
{
Model = Model.ChatGPTTurbo,
Temperature = 0.1,
MaxTokens = 5,
Messages = new ChatMessage[] {
new ChatMessage(ChatMessageRole.User, "Hello!")
}
}).Result;
Assert.IsNotNull(results);
if (results.CreatedUnixTime.HasValue)
{
Assert.NotZero(results.CreatedUnixTime.Value);
Assert.NotNull(results.Created);
Assert.Greater(results.Created.Value, new DateTime(2018, 1, 1));
Assert.Less(results.Created.Value, DateTime.Now.AddDays(1));
}
else
{
Assert.Null(results.Created);
}
Assert.NotNull(results.Object);
Assert.NotNull(results.Choices);
Assert.NotZero(results.Choices.Count);
Assert.AreEqual(ChatMessageRole.Assistant, results.Choices[0].Message.Role);
Assert.That(results.Choices.All(c => c.Message.Role.Equals(ChatMessageRole.Assistant)));
Assert.That(results.Choices.All(c => c.Message.Content.Length > 1));
}
[Test]
public void SimpleCompletion()
{
var api = new OpenAI_API.OpenAIAPI();
Assert.IsNotNull(api.Chat);
var results = api.Chat.CreateChatCompletionAsync("Hello!").Result;
Assert.IsNotNull(results);
if (results.CreatedUnixTime.HasValue)
{
Assert.NotZero(results.CreatedUnixTime.Value);
Assert.NotNull(results.Created);
Assert.Greater(results.Created.Value, new DateTime(2018, 1, 1));
Assert.Less(results.Created.Value, DateTime.Now.AddDays(1));
}
else
{
Assert.Null(results.Created);
}
Assert.NotNull(results.Object);
Assert.NotNull(results.Choices);
Assert.NotZero(results.Choices.Count);
Assert.AreEqual(ChatMessageRole.Assistant, results.Choices[0].Message.Role);
Assert.That(results.Choices.All(c => c.Message.Role.Equals(ChatMessageRole.Assistant)));
Assert.That(results.Choices.All(c => c.Message.Role == ChatMessageRole.Assistant));
Assert.That(results.Choices.All(c => c.Message.Content.Length > 1));
Assert.IsNotEmpty(results.ToString());
}
[Test]
public void ChatBackAndForth()
{
var api = new OpenAI_API.OpenAIAPI();
var chat = api.Chat.CreateConversation();
chat.AppendSystemMessage("You are a teacher who helps children understand if things are animals or not. If the user tells you an animal, you say \"yes\". If the user tells you something that is not an animal, you say \"no\". You only ever respond with \"yes\" or \"no\". You do not say anything else.");
chat.AppendUserInput("Is this an animal? Cat");
chat.AppendExampleChatbotOutput("Yes");
chat.AppendUserInput("Is this an animal? House");
chat.AppendExampleChatbotOutput("No");
chat.AppendUserInput("Is this an animal? Dog");
string res = chat.GetResponseFromChatbotAsync().Result;
Assert.NotNull(res);
Assert.IsNotEmpty(res);
Assert.AreEqual("Yes", res.Trim());
chat.AppendUserInput("Is this an animal? Chair");
res = chat.GetResponseFromChatbotAsync().Result;
Assert.NotNull(res);
Assert.IsNotEmpty(res);
Assert.AreEqual("No", res.Trim());
foreach (ChatMessage msg in chat.Messages)
{
Console.WriteLine($"{msg.Role}: {msg.Content}");
}
}
[Test]
public async Task StreamCompletionEnumerableAsync_ShouldStreamData()
{
var api = new OpenAI_API.OpenAIAPI();
Assert.IsNotNull(api.Chat);
var req = new ChatRequest()
{
Model = Model.ChatGPTTurbo,
Temperature = 0.2,
MaxTokens = 500,
Messages = new ChatMessage[] {
new ChatMessage(ChatMessageRole.User, "Please explain how mountains are formed in great detail.")
}
};
var chatResults = new List<ChatResult>();
await foreach (var res in api.Chat.StreamChatEnumerableAsync(req))
{
chatResults.Add(res);
}
Assert.Greater(chatResults.Count, 100);
Assert.That(chatResults.Select(cr => cr.Choices[0].Delta.Content).Count(c => !string.IsNullOrEmpty(c)) > 50);
}
}
}