Skip to content

Commit

Permalink
updated project to 2020 LTS
Browse files Browse the repository at this point in the history
updated packages and dependencies
fixed asset config setup
added additional completion parameters
  • Loading branch information
StephenHodgson committed Apr 14, 2022
1 parent 3fb185c commit 97c3eba
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Runtime/Completions/Choice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public sealed class Choice
/// <summary>
/// Gets the main text of this completion
/// </summary>
public override string ToString() => $"{Index}|{FinishReason}|{Text}";
public override string ToString() => $"{{\"index\":{Index},\"finish_reason\":\"{FinishReason}\",\"text\":\"{Text}\"}}";

public static implicit operator string(Choice choice) => choice.Text;
}
Expand Down
26 changes: 25 additions & 1 deletion Runtime/Completions/CompletionRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace OpenAI
Expand Down Expand Up @@ -146,6 +147,21 @@ public string StopSequence
}
}

/// <summary>
/// The logit bias dictionary of token bias values for modifying likelihoods. Positive biases increases the likelihood of
/// generating a token, while negative biases decreases the probability of a token. Very large biases (e.g. -100,100) can
/// either eliminate or force a token to be generated.
/// </summary>
[JsonProperty("logit_bias")]
public Dictionary<string, double> LogitBias { get; set; }

/// <summary>
/// How many different completions to generate. Interacts with <see cref="NumChoicesPerPrompt"/> to generate top
/// NumChoicesPerPrompt out of BestOf. In cases where both are set, BestOf should be greater than NumChoicesPerPrompt.
/// </summary>
[JsonProperty("best_of")]
public int? BestOf { get; set; }

/// <summary>
/// This allows you to set default parameters for every request, for example to set a default temperature or max tokens.
/// For every request, if you do not have a parameter set on the request but do have it set here as a default,
Expand Down Expand Up @@ -180,6 +196,8 @@ public CompletionRequest(CompletionRequest basedOn)
LogProbabilities = basedOn.LogProbabilities ?? DefaultCompletionRequestArgs?.LogProbabilities;
Echo = basedOn.Echo ?? DefaultCompletionRequestArgs?.Echo;
StopSequences = basedOn.StopSequences ?? DefaultCompletionRequestArgs?.StopSequences;
LogitBias = basedOn.LogitBias ?? DefaultCompletionRequestArgs?.LogitBias;
BestOf = basedOn.BestOf ?? DefaultCompletionRequestArgs?.BestOf;
}

/// <summary>
Expand Down Expand Up @@ -207,6 +225,8 @@ public CompletionRequest(CompletionRequest basedOn)
/// <param name="echo">Echo back the prompt in addition to the completion.</param>
/// <param name="stopSequences">One or more sequences where the API will stop generating further tokens.
/// The returned text will not contain the stop sequence.</param>
/// <param name="logitBias">A dictionary of logit bias to influence the probability of generating a token.</param>
/// <param name="bestOf">Returns the top bestOf results based on the best probability.</param>
public CompletionRequest(
string prompt = null,
string[] prompts = null,
Expand All @@ -218,7 +238,9 @@ public CompletionRequest(
double? frequencyPenalty = null,
int? logProbabilities = null,
bool? echo = null,
string[] stopSequences = null)
string[] stopSequences = null,
Dictionary<string, double> logitBias = null,
int? bestOf = null)
{
if (prompt != null)
{
Expand All @@ -242,6 +264,8 @@ public CompletionRequest(
LogProbabilities = logProbabilities ?? DefaultCompletionRequestArgs?.LogProbabilities;
Echo = echo ?? DefaultCompletionRequestArgs?.Echo;
StopSequences = stopSequences ?? DefaultCompletionRequestArgs?.StopSequences;
LogitBias = logitBias ?? DefaultCompletionRequestArgs?.LogitBias;
BestOf = bestOf ?? DefaultCompletionRequestArgs?.BestOf;
}
}
}
9 changes: 7 additions & 2 deletions Runtime/OpenAIAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.IO;
using System.Linq;
using UnityEngine;

namespace OpenAI
{
Expand Down Expand Up @@ -53,9 +54,13 @@ public static OpenAIAuthentication Default
}

private static OpenAIAuthentication LoadFromAsset()
=> (from asset in UnityEngine.Object.FindObjectsOfType<OpenAIConfigurationSettings>()
where !string.IsNullOrWhiteSpace(asset.ApiKey) && !string.IsNullOrWhiteSpace(asset.ApiKey)
{
var assets = UnityEngine.Object.FindObjectsOfType<OpenAIConfigurationSettings>().ToList();
assets.AddRange(Resources.FindObjectsOfTypeAll<OpenAIConfigurationSettings>().ToList());
return (from asset in assets where !string.IsNullOrWhiteSpace(asset.ApiKey) &&
!string.IsNullOrWhiteSpace(asset.ApiKey)
select new OpenAIAuthentication(asset.ApiKey)).FirstOrDefault();
}


/// <summary>
Expand Down
16 changes: 12 additions & 4 deletions Tests/EnginesTestFixture.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using NUnit.Framework;
using System.Collections;
using UnityEngine;
using UnityEngine.TestTools;

namespace OpenAI.Tests
Expand All @@ -27,15 +29,21 @@ public IEnumerator Test_2_RetrieveEngineDetails()
{
yield return AwaitTestUtilities.Await(async () =>
{
var api = new OpenAIClient(Engine.Davinci);
var api = new OpenAIClient();
var engines = await api.EnginesEndpoint.GetEnginesAsync();
Assert.IsNotEmpty(engines);
foreach (var engine in engines)
{
var result = await api.EnginesEndpoint.GetEngineDetailsAsync(engine.EngineName);
Assert.IsNotNull(result);
try
{
var result = await api.EnginesEndpoint.GetEngineDetailsAsync(engine.EngineName);
Assert.IsNotNull(result);
}
catch (Exception e)
{
Debug.LogWarning($"Failed to retrieve engine data for {engine.EngineName}\n{e}");
}
}
});
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"displayName": "OpenAI",
"description": "An OpenAI package for the Unity Game Engine.",
"keywords": [],
"version": "1.0.0-preview.1",
"unity": "2019.4",
"version": "1.0.0",
"unity": "2020.3",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -15,7 +15,7 @@
"url": "https://github.com/StephenHodgson"
},
"dependencies": {
"com.unity.nuget.newtonsoft-json": "2.0.1-preview.1"
"com.unity.nuget.newtonsoft-json": "3.0.2"
},
"publishConfig": {
"registry": "https://package.openupm.com"
Expand Down

0 comments on commit 97c3eba

Please sign in to comment.