-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathCompletionResult.cs
121 lines (106 loc) · 3.13 KB
/
CompletionResult.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
using Newtonsoft.Json;
using System.Collections.Generic;
namespace OpenAI_API.Completions
{
/// <summary>
/// Represents a completion choice returned by the Completion API.
/// </summary>
public class Choice
{
/// <summary>
/// The main text of the completion
/// </summary>
[JsonProperty("text")]
public string Text { get; set; }
/// <summary>
/// If multiple completion choices we returned, this is the index withing the various choices
/// </summary>
[JsonProperty("index")]
public int Index { get; set; }
/// <summary>
/// If the request specified <see cref="CompletionRequest.Logprobs"/>, this contains the list of the most likely tokens.
/// </summary>
[JsonProperty("logprobs")]
public Logprobs Logprobs { get; set; }
/// <summary>
/// If this is the last segment of the completion result, this specifies why the completion has ended.
/// </summary>
[JsonProperty("finish_reason")]
public string FinishReason { get; set; }
/// <summary>
/// Gets the main text of this completion
/// </summary>
public override string ToString()
{
return Text;
}
}
/// <summary>
/// API usage as reported by the OpenAI API for this request
/// </summary>
public class CompletionUsage : Usage
{
/// <summary>
/// How many tokens are in the completion(s)
/// </summary>
[JsonProperty("completion_tokens")]
public short CompletionTokens { get; set; }
}
/// <summary>
/// Represents a result from calling the Completion API
/// </summary>
public class CompletionResult : ApiResultBase
{
/// <summary>
/// The identifier of the result, which may be used during troubleshooting
/// </summary>
[JsonProperty("id")]
public string Id { get; set; }
/// <summary>
/// The completions returned by the API. Depending on your request, there may be 1 or many choices.
/// </summary>
[JsonProperty("choices")]
public List<Choice> Completions { get; set; }
/// <summary>
/// API token usage as reported by the OpenAI API for this request
/// </summary>
[JsonProperty("usage")]
public CompletionUsage Usage { get; set; }
/// <summary>
/// Gets the text of the first completion, representing the main result
/// </summary>
public override string ToString()
{
if (Completions != null && Completions.Count > 0)
return Completions[0].ToString();
else
return $"CompletionResult {Id} has no valid output";
}
}
/// <summary>
/// Represents the logprobs property of a completion choice
/// </summary>
public class Logprobs
{
/// <summary>
/// The tokens that were used to generate the completion
/// </summary>
[JsonProperty("tokens")]
public List<string> Tokens { get; set; }
/// <summary>
/// The log probabilities of each token
/// </summary>
[JsonProperty("token_logprobs")]
public List<double?> TokenLogprobs { get; set; }
/// <summary>
/// The top log probabilities for each token
/// </summary>
[JsonProperty("top_logprobs")]
public IList<IDictionary<string, double>> TopLogprobs { get; set; }
/// <summary>
/// The token ids of the tokens
/// </summary>
[JsonProperty("text_offset")]
public List<int> TextOffsets { get; set; }
}
}