Skip to content

Commit

Permalink
Merge branch 'issue-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
jefim committed Jan 5, 2024
2 parents a0bb293 + c7a1fb4 commit b946d09
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 60 deletions.
5 changes: 5 additions & 0 deletions Frends.OpenAI.CallChatGPT/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.0.1] - 2024-01-05
### Changed
- Moved some input parameters to options
- Moved API key from options to input

## [1.0.0] - 2023-12-28
### Changed
- Initial implementation
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,29 @@ public void InputProperties_SetAndGetCorrectly()
// Act
input.Model = "gpt-3.5-turbo";
input.Messages = messages;
input.MaxTokens = 100;
input.N = 3;
input.Seed = 12345;
input.User = "test-user";

// Assert
Assert.AreEqual("gpt-3.5-turbo", input.Model);
CollectionAssert.AreEqual(messages, input.Messages);
Assert.AreEqual(100, input.MaxTokens);
Assert.AreEqual(3, input.N);
Assert.AreEqual(12345, input.Seed);
Assert.AreEqual("test-user", input.User);
}

[Test]
public void OptionsProperties_SetAndGetCorrectly()
{
// Arrange
var options = new Options();

// Act
options.MaxTokens = 100;
options.N = 3;
options.Seed = 12345;
options.User = "test-user";

// Assert
Assert.AreEqual(100, options.MaxTokens);
Assert.AreEqual(3, options.N);
Assert.AreEqual(12345, options.Seed);
Assert.AreEqual("test-user", options.User);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public class Input
[DefaultValue("gpt-3.5-turbo")]
public string Model { get; set; }

/// <summary>
/// Chat GPT API key.
/// </summary>
/// <example>1234567qwerty</example>
[DisplayFormat(DataFormatString = "Text")]
[PasswordPropertyText]
public string ApiKey { get; set; }

/// <summary>
/// Array of messages to send to Chat GPT.
/// </summary>
Expand All @@ -27,49 +35,6 @@ public class Input
/// ]
/// </example>
public InputMessage[] Messages { get; set; }

/// <summary>
/// The maximum number of tokens that can be generated in the chat
/// completion. Set to 0 for unlimited tokens.
/// The total length of input tokens and generated tokens is still limited
/// by the model's context length.
/// </summary>
/// <example>100</example>
[DefaultValue(null)]
public int? MaxTokens { get; set; }

/// <summary>
/// How many chat completion choices to generate for each input message.
/// Note that you will be charged based on the number of generated tokens
/// across all of the choices. Keep n as 1 to minimize costs.
/// </summary>
/// <internalnote>
/// This property name comes directly from ChatGPT API, thus is called
/// just N and not somethings else. We want to keep the task as close to
/// the API as possible.
/// </internalnote>
/// <example>3</example>
[DefaultValue(1)]
public int N { get; set; } = 1;

/// <summary>
/// If specified, ChatGPT system will make a best effort to sample
/// deterministically, such that repeated requests with the same seed and
/// parameters should return the same result. Determinism is not guaranteed,
/// and you should refer to the system_fingerprint response parameter to
/// monitor changes in the backend.
/// Set to null for non-deterministic results.
/// </summary>
/// <example>12345</example>
[DefaultValue(null)]
public int? Seed { get; set; }

/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to
/// monitor and detect abuse.
/// </summary>
/// <example>User12345</example>
public string User { get; set; }
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,47 @@
public class Options
{
/// <summary>
/// Chat GPT API key.
/// The maximum number of tokens that can be generated in the chat
/// completion. Set to 0 for unlimited tokens.
/// The total length of input tokens and generated tokens is still limited
/// by the model's context length.
/// </summary>
/// <example>1234567qwerty</example>
[DisplayFormat(DataFormatString = "Text")]
[PasswordPropertyText]
public string ApiKey { get; set; }
/// <example>100</example>
[DefaultValue(null)]
public int? MaxTokens { get; set; }

/// <summary>
/// How many chat completion choices to generate for each input message.
/// Note that you will be charged based on the number of generated tokens
/// across all of the choices. Keep n as 1 to minimize costs.
/// </summary>
/// <internalnote>
/// This property name comes directly from ChatGPT API, thus is called
/// just N and not somethings else. We want to keep the task as close to
/// the API as possible.
/// </internalnote>
/// <example>3</example>
[DefaultValue(1)]
public int N { get; set; } = 1;

/// <summary>
/// If specified, ChatGPT system will make a best effort to sample
/// deterministically, such that repeated requests with the same seed and
/// parameters should return the same result. Determinism is not guaranteed,
/// and you should refer to the system_fingerprint response parameter to
/// monitor changes in the backend.
/// Set to null for non-deterministic results.
/// </summary>
/// <example>12345</example>
[DefaultValue(null)]
public int? Seed { get; set; }

/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to
/// monitor and detect abuse.
/// </summary>
/// <example>User12345</example>
public string User { get; set; }

/// <summary>
/// Whether to throw an exception if the API returns an error. Otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@ public static async Task<Result> CallChatGPT(
[PropertyTab] Options options,
CancellationToken cancellationToken)
{
using var client = CreateClient(options);
using var client = CreateClient(input);
var request = new RestRequest("v1/chat/completions");
request.AddJsonBody(input);
request.AddJsonBody(new
{
input.Messages,
input.Model,
options.MaxTokens,
options.N,
options.Seed,
options.User,
});

var response = await client.ExecutePostAsync<ChatCompletion>(request, cancellationToken);
return response.IsSuccessful
Expand All @@ -69,9 +77,9 @@ private static Result HandleErrorResponse(Options options, RestResponse<ChatComp
return new Result(false, errorText, null);
}

private static RestClient CreateClient(Options options)
private static RestClient CreateClient(Input input)
{
var auth = new JwtAuthenticator(options.ApiKey);
var auth = new JwtAuthenticator(input.ApiKey);
var restClientOptions = new RestClientOptions
{
// S1075 is complaining about hardcoded URL, but this is the host
Expand Down

0 comments on commit b946d09

Please sign in to comment.