Skip to content

Commit

Permalink
feat: Added Tool generator support.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jul 29, 2024
1 parent a5cf7bc commit 72a5088
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 105 deletions.
5 changes: 0 additions & 5 deletions src/helpers/FixOpenApiSpec/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@
},
},
};
openApiDocument.Components.Schemas["ToolCallFunction"]!.Properties["arguments"] = new OpenApiSchema
{
Type = "string",
};


text = openApiDocument.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
_ = new OpenApiStringReader().Read(text, out diagnostics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace Ollama.Generators;

[Generator]
public class OllamaFunctionsGenerator : IIncrementalGenerator
public class OllamaToolsGenerator : IIncrementalGenerator
{
#region Constants

public const string Name = nameof(OllamaFunctionsGenerator);
public const string Name = nameof(OllamaToolsGenerator);
public const string Id = "OAFG";

#endregion
Expand All @@ -22,7 +22,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
var attributes =
context.SyntaxProvider
.ForAttributeWithMetadataName("Ollama.OllamaFunctionsAttribute")
.ForAttributeWithMetadataName("Ollama.OllamaToolsAttribute")
.SelectManyAllAttributesOfCurrentInterfaceSyntax()
.SelectAndReportExceptions(PrepareData, context, Id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ namespace Ollama;
/// </summary>
[AttributeUsage(AttributeTargets.Interface)]
[System.Diagnostics.Conditional("OLLAMA_FUNCTIONS_ATTRIBUTES")]
public sealed class OllamaFunctionsAttribute : Attribute;
public sealed class OllamaToolsAttribute : Attribute;
10 changes: 10 additions & 0 deletions src/libs/Ollama/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,14 @@ public static Message AsToolMessage(this string content)
Content = content,
};
}

/// <summary>
///
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public static string AsJson(this ToolCallFunctionArgs args)
{
return JsonSerializer.Serialize(args, SourceGenerationContext.Default.ToolCallFunctionArgs);
}
}
60 changes: 32 additions & 28 deletions src/libs/Ollama/Generated/JsonSerializerContextTypes.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,114 +93,118 @@ public sealed partial class JsonSerializerContextTypes
/// <summary>
///
/// </summary>
public global::System.Collections.Generic.IList<double>? Type20 { get; set; }
public global::Ollama.ToolCallFunctionArgs? Type20 { get; set; }
/// <summary>
///
/// </summary>
public global::System.AnyOf<string?, global::Ollama.CreateModelResponseStatus?>? Type21 { get; set; }
public global::System.Collections.Generic.IList<double>? Type21 { get; set; }
/// <summary>
///
/// </summary>
public global::System.Collections.Generic.IList<global::Ollama.Model>? Type22 { get; set; }
public global::System.AnyOf<string?, global::Ollama.CreateModelResponseStatus?>? Type22 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.ModelDetails? Type23 { get; set; }
public global::System.Collections.Generic.IList<global::Ollama.Model>? Type23 { get; set; }
/// <summary>
///
/// </summary>
public global::System.Collections.Generic.IList<global::Ollama.ProcessModel>? Type24 { get; set; }
public global::Ollama.ModelDetails? Type24 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.ModelInformation? Type25 { get; set; }
public global::System.Collections.Generic.IList<global::Ollama.ProcessModel>? Type25 { get; set; }
/// <summary>
///
/// </summary>
public global::System.AnyOf<string?, global::Ollama.PullModelResponseStatus?>? Type26 { get; set; }
public global::Ollama.ModelInformation? Type26 { get; set; }
/// <summary>
///
/// </summary>
public global::System.AnyOf<string?, global::Ollama.PushModelResponseStatus?>? Type27 { get; set; }
public global::System.AnyOf<string?, global::Ollama.PullModelResponseStatus?>? Type27 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.VersionResponse? Type28 { get; set; }
public global::System.AnyOf<string?, global::Ollama.PushModelResponseStatus?>? Type28 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.GenerateCompletionRequest? Type29 { get; set; }
public global::Ollama.VersionResponse? Type29 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.GenerateCompletionResponse? Type30 { get; set; }
public global::Ollama.GenerateCompletionRequest? Type30 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.GenerateChatCompletionRequest? Type31 { get; set; }
public global::Ollama.GenerateCompletionResponse? Type31 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.GenerateChatCompletionResponse? Type32 { get; set; }
public global::Ollama.GenerateChatCompletionRequest? Type32 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.GenerateEmbeddingRequest? Type33 { get; set; }
public global::Ollama.GenerateChatCompletionResponse? Type33 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.GenerateEmbeddingResponse? Type34 { get; set; }
public global::Ollama.GenerateEmbeddingRequest? Type34 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.CreateModelRequest? Type35 { get; set; }
public global::Ollama.GenerateEmbeddingResponse? Type35 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.CreateModelResponse? Type36 { get; set; }
public global::Ollama.CreateModelRequest? Type36 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.ModelsResponse? Type37 { get; set; }
public global::Ollama.CreateModelResponse? Type37 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.ProcessResponse? Type38 { get; set; }
public global::Ollama.ModelsResponse? Type38 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.ModelInfoRequest? Type39 { get; set; }
public global::Ollama.ProcessResponse? Type39 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.ModelInfo? Type40 { get; set; }
public global::Ollama.ModelInfoRequest? Type40 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.CopyModelRequest? Type41 { get; set; }
public global::Ollama.ModelInfo? Type41 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.DeleteModelRequest? Type42 { get; set; }
public global::Ollama.CopyModelRequest? Type42 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.PullModelRequest? Type43 { get; set; }
public global::Ollama.DeleteModelRequest? Type43 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.PullModelResponse? Type44 { get; set; }
public global::Ollama.PullModelRequest? Type44 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.PushModelRequest? Type45 { get; set; }
public global::Ollama.PullModelResponse? Type45 { get; set; }
/// <summary>
///
/// </summary>
public global::Ollama.PushModelResponse? Type46 { get; set; }
public global::Ollama.PushModelRequest? Type46 { get; set; }
/// <summary>
///
/// </summary>
public byte[]? Type47 { get; set; }
public global::Ollama.PushModelResponse? Type47 { get; set; }
/// <summary>
///
/// </summary>
public byte[]? Type48 { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/libs/Ollama/Generated/Ollama.Models.ToolCallFunction.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public sealed partial class ToolCallFunction
public required string Name { get; set; }

/// <summary>
///
/// The arguments to pass to the function.
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("arguments")]
[global::System.Text.Json.Serialization.JsonRequired]
public required string Arguments { get; set; }
public required global::Ollama.ToolCallFunctionArgs Arguments { get; set; }

/// <summary>
/// Additional properties that are not explicitly defined in the schema
Expand Down
9 changes: 9 additions & 0 deletions src/libs/Ollama/JsonSerializerContextTypes.AdditionalTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Ollama;

public partial class JsonSerializerContextTypes
{
/// <summary>
///
/// </summary>
public JsonElement JsonElement { get; set; }
}
5 changes: 4 additions & 1 deletion src/libs/Ollama/OllamaApiClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public static async Task<GenerateChatCompletionResponse> WaitAsync(
enumerable = enumerable ?? throw new ArgumentNullException(nameof(enumerable));

MessageRole? responseRole = null;
IList<ToolCall>? toolCalls = null;
var responseContent = new StringBuilder();

var currentResponse = new GenerateChatCompletionResponse
Expand All @@ -123,6 +124,7 @@ public static async Task<GenerateChatCompletionResponse> WaitAsync(
await foreach (var response in enumerable.ConfigureAwait(false))
{
responseRole ??= response.Message.Role;
toolCalls ??= response.Message.ToolCalls;
responseContent.Append(response.Message.Content);

currentResponse = response;
Expand All @@ -131,7 +133,8 @@ public static async Task<GenerateChatCompletionResponse> WaitAsync(
currentResponse.Message = new Message
{
Role = responseRole ?? MessageRole.User,
Content = responseContent.ToString()
Content = responseContent.ToString(),
ToolCalls = toolCalls,
};

return currentResponse;
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Ollama/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ components:
type: string
description: The name of the function to be called.
arguments:
type: string
$ref: '#/components/schemas/ToolCallFunctionArgs'
description: The function the model wants to call.
ToolCallFunctionArgs:
type: object
Expand Down
Loading

0 comments on commit 72a5088

Please sign in to comment.