Skip to content

Commit

Permalink
Update ChatGptClient to support tools
Browse files Browse the repository at this point in the history
  • Loading branch information
selfdocumentingcode committed Aug 2, 2024
1 parent 442f9b7 commit efea5f2
Show file tree
Hide file tree
Showing 20 changed files with 361 additions and 145 deletions.
54 changes: 27 additions & 27 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true

# C# files
Expand Down Expand Up @@ -81,78 +80,78 @@ dotnet_naming_style.pascal_case.capitalization = pascal_case

# Readability

dotnet_diagnostic.SA1101.severity = none # Prefix local calls with this
dotnet_diagnostic.sa1101.severity = none # Prefix local calls with this

# Ordering

dotnet_diagnostic.SA1200.severity = none # Using directive should appear within a namespace declaration
dotnet_diagnostic.SA1201.severity = suggestion # Elements should appear in the correct order
dotnet_diagnostic.SA1202.severity = suggestion # Elements should be ordered by access
dotnet_diagnostic.sa1200.severity = none # Using directive should appear within a namespace declaration
dotnet_diagnostic.sa1201.severity = suggestion # Elements should appear in the correct order
dotnet_diagnostic.sa1202.severity = suggestion # Elements should be ordered by access

# Naming

dotnet_diagnostic.SA1309.severity = none # Field names should not begin with underscore
dotnet_diagnostic.SA1312.severity = none # Variable names must being with lower-case letter
dotnet_diagnostic.sa1309.severity = none # Field names should not begin with underscore
dotnet_diagnostic.sa1312.severity = none # Variable names must being with lower-case letter

# Maintainability

dotnet_diagnostic.SA1402.severity = suggestion # File may only contain a single type
dotnet_diagnostic.sa1402.severity = suggestion # File may only contain a single type

# Layout rules

dotnet_diagnostic.SA1502.severity = none # Element should not be on a single line
dotnet_diagnostic.SA1503.severity = none # Braces should not be omitted
dotnet_diagnostic.SA1515.severity = suggestion # Single-line comment should be preceded by blank line
dotnet_diagnostic.sa1502.severity = none # Element should not be on a single line
dotnet_diagnostic.sa1503.severity = none # Braces should not be omitted
dotnet_diagnostic.sa1515.severity = suggestion # Single-line comment should be preceded by blank line
# Documentation

dotnet_diagnostic.SA1600.severity = none # Elements should be documented
dotnet_diagnostic.SA1601.severity = none # Partial elements should be documented
dotnet_diagnostic.SA1602.severity = none # Enumeration items should be documented
dotnet_diagnostic.SA1611.severity = none # Element parameter should be documented
dotnet_diagnostic.SA1615.severity = none # Element return value should be documented
dotnet_diagnostic.SA1633.severity = none # File should have header
dotnet_diagnostic.SA1649.severity = none # File name should match first type name
dotnet_diagnostic.sa1600.severity = none # Elements should be documented
dotnet_diagnostic.sa1601.severity = none # Partial elements should be documented
dotnet_diagnostic.sa1602.severity = none # Enumeration items should be documented
dotnet_diagnostic.sa1611.severity = none # Element parameter should be documented
dotnet_diagnostic.sa1615.severity = none # Element return value should be documented
dotnet_diagnostic.sa1633.severity = none # File should have header
dotnet_diagnostic.sa1649.severity = none # File name should match first type name

### .NET Code-style rules ###

# Expression-level preferences

dotnet_diagnostic.IDE0032.severity = suggestion # Use auto-implemented property
dotnet_diagnostic.ide0032.severity = suggestion # Use auto-implemented property

# Modifier preferences

dotnet_diagnostic.IDE0044.severity = warning # Add readonly modifier
dotnet_diagnostic.ide0044.severity = warning # Add readonly modifier

# Naming

dotnet_diagnostic.IDE1006.severity = warning # Naming rule violation
dotnet_diagnostic.ide1006.severity = warning # Naming rule violation

# Unnecessary code rules

dotnet_diagnostic.IDE0058.severity = none # Remove unnecessary expression value
dotnet_diagnostic.IDE0290.severity = none # Use primary constructor
dotnet_diagnostic.ide0058.severity = none # Remove unnecessary expression value
dotnet_diagnostic.ide0290.severity = none # Use primary constructor

### .NET Code-quality rules ###

# Reliability

dotnet_diagnostic.CA2016.severity = warning # Forward the CancellationToken parameter to methods that take one
dotnet_diagnostic.ca2016.severity = warning # Forward the CancellationToken parameter to methods that take one

# Usage

dotnet_diagnostic.CA2208.severity = warning # Instantiate argument exceptions correctly
dotnet_diagnostic.ca2208.severity = warning # Instantiate argument exceptions correctly

### C# compiler messages ###

# Level 4 warning messages

dotnet_diagnostic.CS1591.severity = none # Missing XML comment for publicly visible type or member
dotnet_diagnostic.cs1591.severity = none # Missing XML comment for publicly visible type or member

### SYSLIB diagnostics ###

# Source-generated code

dotnet_diagnostic.SYSLIB1045.severity = none # Convert to 'GeneratedRegexAttribute'.
dotnet_diagnostic.syslib1045.severity = none # Convert to 'GeneratedRegexAttribute'.

### Rider auto-generated rules ###

Expand Down Expand Up @@ -191,6 +190,7 @@ resharper_nested_ternary_style = expanded
resharper_parentheses_same_type_operations = true
resharper_place_accessorholder_attribute_on_same_line = false
resharper_place_expr_property_on_single_line = true
resharper_place_field_attribute_on_same_line = false
resharper_place_simple_embedded_statement_on_same_line = true
resharper_place_simple_initializer_on_single_line = false
resharper_trailing_comma_in_multiline_lists = true
28 changes: 28 additions & 0 deletions Kattbot.Common/Models/KattGpt/ChatCompletionChoice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Text.Json.Serialization;

namespace Kattbot.Common.Models.KattGpt;

public record ChatCompletionChoice
{
/// <summary>
/// The index of the choice in the list of choices.
/// </summary>
[JsonPropertyName("index")]
public int Index { get; set; }

Check warning on line 11 in Kattbot.Common/Models/KattGpt/ChatCompletionChoice.cs

View workflow job for this annotation

GitHub Actions / Build

The property's documentation summary text should begin with: 'Gets or sets' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1623.md)

/// <summary>
/// A chat completion message generated by the model.
/// </summary>
[JsonPropertyName("message")]
public ChatCompletionMessage Message { get; set; } = null!;

Check warning on line 17 in Kattbot.Common/Models/KattGpt/ChatCompletionChoice.cs

View workflow job for this annotation

GitHub Actions / Build

The property's documentation summary text should begin with: 'Gets or sets' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1623.md)

/// <summary>
/// The reason the model stopped generating tokens.
/// This will be 'stop' if the model hit a natural stop point or a provided stop sequence,
/// 'length' if the maximum number of tokens specified in the request was reached,
/// 'content_filter' if content was omitted due to a flag from our content filters
/// or 'tool_calls' if the model called a tool.
/// </summary>
[JsonPropertyName("finish_reason")]
public ChoiceFinishReason FinishReason { get; set; }

Check warning on line 27 in Kattbot.Common/Models/KattGpt/ChatCompletionChoice.cs

View workflow job for this annotation

GitHub Actions / Build

The property's documentation summary text should begin with: 'Gets or sets' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1623.md)
}
40 changes: 28 additions & 12 deletions Kattbot.Common/Models/KattGpt/ChatCompletionCreateRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Kattbot.Common.Models.KattGpt;

/// <summary>
/// Creates a model response for the given chat conversation.
/// https://platform.openai.com/docs/api-reference/chat/create

Check warning on line 7 in Kattbot.Common/Models/KattGpt/ChatCompletionCreateRequest.cs

View workflow job for this annotation

GitHub Actions / Build

/// </summary>
public record ChatCompletionCreateRequest
{
/// <summary>
Expand All @@ -19,22 +23,34 @@ public record ChatCompletionCreateRequest
public ChatCompletionMessage[] Messages { get; set; } = null!;

/// <summary>
/// Gets or sets a list of functions the model may generate JSON inputs for.
/// https://platform.openai.com/docs/api-reference/chat/create#functions.
/// A list of tools the model may call. Currently, only functions are supported as a tool.
/// Use this to provide a list of functions the model may generate JSON inputs for.
/// A max of 128 functions are supported.
/// https://platform.openai.com/docs/api-reference/chat/create#chat-create-tools
/// </summary>
[JsonPropertyName("functions")]
public ChatCompletionFunction[]? Functions { get; set; }
[JsonPropertyName("tools")]
public ChatCompletionTool[]? Tools { get; set; }

/// <summary>
/// Gets or sets the mode for controlling the model responds to function calls. none means the model does not call a
/// function,
/// and responds to the end-user. auto means the model can pick between an end-user or calling a function.
/// Specifying a particular function via {"name": "my_function"} forces the model to call that function.
/// Defaults to "none" when no functions are present and "auto" if functions are present.
/// https://platform.openai.com/docs/api-reference/chat/create#function_call.
/// Controls which (if any) tool is called by the model.
/// none means the model will not call any tool and instead generates a message.
/// auto means the model can pick between generating a message or calling one or more tools.
/// required means the model must call one or more tools.
/// Specifying a particular tool via {"type": "function", "function": {"name": "my_function"}}
/// forces the model to call that tool.
/// none is the default when no tools are present. auto is the default if tools are present.
/// https://platform.openai.com/docs/api-reference/chat/create#chat-create-tool_choice
/// </summary>
[JsonPropertyName("function_call")]
public string? FunctionCall { get; set; }
[JsonPropertyName("tool_choice")]
public StringOrObject<ChatCompletionToolChoice> ToolChoice { get; set; }

Check warning on line 45 in Kattbot.Common/Models/KattGpt/ChatCompletionCreateRequest.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'ToolChoice' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <summary>
/// Whether to enable parallel function calling during tool use.
/// Defaults to true
/// https://platform.openai.com/docs/api-reference/chat/create#chat-create-parallel_tool_calls
/// </summary>
[JsonPropertyName("parallel_tool_calls")]
public bool ParallelToolCalls { get; set; } = false;

/// <summary>
/// Gets or sets what sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more
Expand Down
10 changes: 5 additions & 5 deletions Kattbot.Common/Models/KattGpt/ChatCompletionCreateResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ namespace Kattbot.Common.Models.KattGpt;
public record ChatCompletionCreateResponse
{
[JsonPropertyName("id")]
public string Id { get; set; } = null!;
public string Id { get; init; } = null!;

[JsonPropertyName("object")]
public string Object { get; set; } = null!;
public string Object { get; init; } = null!;

[JsonPropertyName("created")]
public int Created { get; set; }
public int Created { get; init; }

[JsonPropertyName("choices")]
public List<Choice> Choices { get; set; } = new();
public List<ChatCompletionChoice> Choices { get; init; } = [];

[JsonPropertyName("usage")]
public Usage Usage { get; set; } = null!;
public ChatCompletionUsage Usage { get; init; } = null!;
}
10 changes: 5 additions & 5 deletions Kattbot.Common/Models/KattGpt/ChatCompletionFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ namespace Kattbot.Common.Models.KattGpt;
public record ChatCompletionFunction
{
/// <summary>
/// Gets or sets the name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with
/// Gets the name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with
/// a maximum length of 64.
/// https://platform.openai.com/docs/api-reference/chat/create#functions-name.
/// </summary>
[JsonPropertyName("name")]
public string Name { get; set; } = null!;
public string Name { get; init; } = null!;

/// <summary>
/// Gets or sets a description of what the function does, used by the model to choose when and how to call the
/// function.
/// https://platform.openai.com/docs/api-reference/chat/create#functions-description.
/// </summary>
[JsonPropertyName("description")]
public string? Description { get; set; }
public string? Description { get; init; }

/// <summary>
/// Gets or sets the parameters the functions accepts, described as a JSON Schema object.
/// Gets the parameters the functions accepts, described as a JSON Schema object.
/// https://platform.openai.com/docs/api-reference/chat/create#functions-parameters.
/// </summary>
[JsonPropertyName("parameters")]
public JsonObject Parameters { get; set; } = null!;
public JsonObject Parameters { get; init; } = null!;
}
27 changes: 27 additions & 0 deletions Kattbot.Common/Models/KattGpt/ChatCompletionFunctionCall.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Text.Json.Serialization;

namespace Kattbot.Common.Models.KattGpt;

public record ChatCompletionFunctionCall
{
public ChatCompletionFunctionCall(string name, string arguments)
{
Name = name;
Arguments = arguments;
}

/// <summary>
/// The name of the function to call.
/// </summary>
[JsonPropertyName("name")]
public string Name { get; }

Check warning on line 17 in Kattbot.Common/Models/KattGpt/ChatCompletionFunctionCall.cs

View workflow job for this annotation

GitHub Actions / Build

The property's documentation summary text should begin with: 'Gets' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1623.md)

/// <summary>
/// The arguments to call the function with, as generated by the model in JSON format.
/// Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your
/// function schema.
/// Validate the arguments in your code before calling your function.
/// </summary>
[JsonPropertyName("arguments")]
public string Arguments { get; }

Check warning on line 26 in Kattbot.Common/Models/KattGpt/ChatCompletionFunctionCall.cs

View workflow job for this annotation

GitHub Actions / Build

The property's documentation summary text should begin with: 'Gets' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1623.md)
}
12 changes: 12 additions & 0 deletions Kattbot.Common/Models/KattGpt/ChatCompletionFunctionChoice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace Kattbot.Common.Models.KattGpt;

public record ChatCompletionFunctionChoice
{
/// <summary>
/// The name of the function to call.
/// </summary>
[JsonPropertyName("name")]
public string Name { get; set; } = null!;

Check warning on line 11 in Kattbot.Common/Models/KattGpt/ChatCompletionFunctionChoice.cs

View workflow job for this annotation

GitHub Actions / Build

The property's documentation summary text should begin with: 'Gets or sets' (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1623.md)
}
Loading

0 comments on commit efea5f2

Please sign in to comment.