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 6dd37af commit ec4f620
Show file tree
Hide file tree
Showing 30 changed files with 1,123 additions and 218 deletions.

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

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

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

20 changes: 14 additions & 6 deletions OpenAI/Packages/com.openai.unity/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 ec4f620

Please sign in to comment.