forked from RageAgainstThePixel/OpenAI-DotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestFixture_02_Completions.cs
76 lines (68 loc) · 2.92 KB
/
TestFixture_02_Completions.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
using NUnit.Framework;
using OpenAI.Completions;
using OpenAI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace OpenAI.Tests
{
internal class TestFixture_02_Completions
{
private const string CompletionPrompts = "One Two Three Four Five Six Seven Eight Nine One Two Three Four Five Six Seven Eight";
[Test]
public async Task Test_01_GetBasicCompletion()
{
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
Assert.IsNotNull(api.CompletionsEndpoint);
var result = await api.CompletionsEndpoint.CreateCompletionAsync(
CompletionPrompts,
temperature: 0.1,
maxTokens: 5,
numOutputs: 5,
model: Model.Davinci);
Assert.IsNotNull(result);
Assert.NotNull(result.Completions);
Assert.NotZero(result.Completions.Count);
Assert.That(result.Completions.Any(c => c.Text.Trim().ToLower().StartsWith("nine")));
Console.WriteLine(result);
}
[Test]
public async Task Test_02_GetStreamingCompletion()
{
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
Assert.IsNotNull(api.CompletionsEndpoint);
var allCompletions = new List<Choice>();
await api.CompletionsEndpoint.StreamCompletionAsync(result =>
{
Assert.IsNotNull(result);
Assert.NotNull(result.Completions);
Assert.NotZero(result.Completions.Count);
allCompletions.AddRange(result.Completions);
}, CompletionPrompts, temperature: 0.1, maxTokens: 5, numOutputs: 5);
Assert.That(allCompletions.Any(c => c.Text.Trim().ToLower().StartsWith("nine")));
Console.WriteLine(allCompletions.FirstOrDefault());
}
[Test]
public async Task Test_03_GetStreamingEnumerableCompletion()
{
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
Assert.IsNotNull(api.CompletionsEndpoint);
var allCompletions = new List<Choice>();
await foreach (var result in api.CompletionsEndpoint.StreamCompletionEnumerableAsync(
CompletionPrompts,
temperature: 0.1,
maxTokens: 5,
numOutputs: 5,
model: Model.Davinci))
{
Assert.IsNotNull(result);
Assert.NotNull(result.Completions);
Assert.NotZero(result.Completions.Count);
allCompletions.AddRange(result.Completions);
}
Assert.That(allCompletions.Any(c => c.Text.Trim().ToLower().StartsWith("nine")));
Console.WriteLine(allCompletions.FirstOrDefault());
}
}
}