Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ reference
/CodeCoverage
/Assets/Resources/BillingMode.json
/Assets/Resources/BillingMode.json.meta

# Secret key
Assets/Resources/openAI.txt
8 changes: 8 additions & 0 deletions Assets/i5 Toolkit for Unity/Runtime/Open AI.meta

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

36 changes: 36 additions & 0 deletions Assets/i5 Toolkit for Unity/Runtime/Open AI/APIKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using UnityEngine;

namespace i5.Toolkit.Core.OpenAI
{
/// <summary>
/// Retrives the API key from the Resources folder. You need to create this folder in Assets and name it EXEACTLY Resources.
/// Then create a file called openAI.txt somewhere and import it into Resources. Finally, enter your API key into openAI.txt.
/// Never commit this file to source control!
/// </summary>
public static class APIKey
{
private static string _key = "";

public static string key
{
get
{
if (_key == "")
{
TextAsset keyAsset = Resources.Load("openAI") as TextAsset;
if (keyAsset != null)
{
_key = keyAsset.text;
_key = _key.Replace("\n", "");
}
else
{
Debug.LogError("Create a Resources folder and import a file called openAI.txt containig your API key!");
}

}
return _key;
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/i5 Toolkit for Unity/Runtime/Open AI/APIKey.cs.meta

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

157 changes: 157 additions & 0 deletions Assets/i5 Toolkit for Unity/Runtime/Open AI/FunctionCalling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;

namespace i5.Toolkit.Core.OpenAI
{
[Serializable]
public class FunctionCallAnswer : Input
{
public string call_id;
public string output;

public FunctionCallAnswer()
{
type = "function_call_output";
}
}

[Serializable]
public class FunctionCallRequest : Input
{
public string call_id;
public string arguments;
public string name;
}

[Serializable]
public class FunctionCall : Tool
{
public string name;
public string description;
public Parameter parameters = new Parameter();
public bool strict = true;
public FunctionCall()
{
type = "function";
}
}

[Serializable]
public class Parameter
{
public string type = "object";
//public Properties properties;
public List<PropertieTemplate> properties = new List<PropertieTemplate>();
//public string[] required = new string[0];
public bool additionalProperties;
}

public class ParameterConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken t = JToken.FromObject(value);

if (t.Type != JTokenType.Object)
{
t.WriteTo(writer);
}
else
{
JObject o = (JObject)t;
Parameter parameters = (Parameter)value;
o.Remove("properties");
JObject properties = new JObject();
foreach (PropertieTemplate propertie in parameters.properties)
{
JObject propertieJSON = new JObject
{
new JProperty("type", propertie.type),
new JProperty("description", propertie.description)
};
properties.Add(new JProperty(propertie.name,propertieJSON));
}

o.Add(new JProperty("properties", properties));
IList<string> required = parameters.properties.Select(p => p.name).ToList();
o.Add(new JProperty("required", new JArray(required)));

o.WriteTo(writer);
}
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
}

public override bool CanRead
{
get { return false; }
}

public override bool CanConvert(Type objectType)
{
return typeof(Parameter).IsAssignableFrom(objectType);
}
}

[Serializable]
public class Properties
{
}

[Serializable]
public class PropertieTemplate
{
public string name;
public string type;
public string description;

public PropertieTemplate(string name, string type, string description)
{
this.name = name;
this.type = type;
this.description = description;
}
}

public abstract class ILLMFunction
{
public string functionName;
public virtual string Work()
{
return "sucess";
}

public virtual void Populate(string json)
{
JsonConvert.PopulateObject(json, this);
}
}

public class LLMTypeAttribute : Attribute
{
public string type { get; private set; }

public LLMTypeAttribute(string type)
{
this.type = type;
}
}

public class LLMDescriptionAttribute : Attribute
{
public string description { get; private set; }

public LLMDescriptionAttribute(string description)
{
this.description = description;
}
}
}

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

Loading