Skip to content

Commit

Permalink
com.openai.unity 5.2.0 (#117)
Browse files Browse the repository at this point in the history
- Updated chat endpoint features
  - json mode
  - gpt-vision
  - reproducible outputs
  - tool functions
  • Loading branch information
StephenHodgson authored Nov 9, 2023
1 parent b630858 commit b15fd8e
Show file tree
Hide file tree
Showing 28 changed files with 1,076 additions and 196 deletions.
2 changes: 1 addition & 1 deletion Runtime/Audio/SpeechRequest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime/Audio/SpeechResponseFormat.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime/Audio/SpeechVoice.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions Runtime/Chat/ChatEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public ChatEndpoint(OpenAIClient client) : base(client) { }
public async Task<ChatResponse> GetCompletionAsync(ChatRequest chatRequest, CancellationToken cancellationToken = default)
{
var payload = JsonConvert.SerializeObject(chatRequest, OpenAIClient.JsonSerializationOptions);

if (EnableDebug)
{
Debug.Log(payload);
}

var response = await Rest.PostAsync(GetUrl("/completions"), payload, new RestParameters(client.DefaultRequestHeaders), cancellationToken);
response.Validate(EnableDebug);
return response.DeserializeResponse<ChatResponse>(response.Body);
Expand All @@ -47,12 +53,16 @@ public async Task<ChatResponse> StreamCompletionAsync(ChatRequest chatRequest, A
{
chatRequest.Stream = true;
ChatResponse chatResponse = null;

var payload = JsonConvert.SerializeObject(chatRequest, OpenAIClient.JsonSerializationOptions);
var response = await Rest.PostAsync(GetUrl("/completions"), payload, eventData =>
{
try
{
if (EnableDebug)
{
Debug.Log(eventData);
}

var partialResponse = JsonConvert.DeserializeObject<ChatResponse>(eventData, OpenAIClient.JsonSerializationOptions);

if (chatResponse == null)
Expand All @@ -64,19 +74,17 @@ public async Task<ChatResponse> StreamCompletionAsync(ChatRequest chatRequest, A
chatResponse.CopyFrom(partialResponse);
}

resultHandler(partialResponse);
resultHandler?.Invoke(partialResponse);
}
catch (Exception e)
{
Debug.LogError($"{eventData}\n{e}");
}
}, new RestParameters(client.DefaultRequestHeaders), cancellationToken);
response.Validate(EnableDebug);

response?.Validate(EnableDebug);
if (chatResponse == null) { return null; }

chatResponse.SetResponseData(response);
resultHandler(chatResponse);
resultHandler?.Invoke(chatResponse);
return chatResponse;
}
}
Expand Down
Loading

0 comments on commit b15fd8e

Please sign in to comment.