Skip to content

Instantly share code, notes, and snippets.

@stonstad
Last active September 14, 2023 13:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stonstad/b1b4e569e76a32094671126bfde56bb6 to your computer and use it in GitHub Desktop.
Save stonstad/b1b4e569e76a32094671126bfde56bb6 to your computer and use it in GitHub Desktop.
Simple GPT-3.5-Turbo C# Client
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace GPTClient
{
class Program
{
static async Task Main(string[] args)
{
string apiUrl = "https://api.openai.com/v1/chat/completions";
string apiKey = "<<replace with your API key>>";
Request request = new Request();
request.Messages = new RequestMessage[]
{
new RequestMessage()
{
Role = "system",
Content = "You are a helpful assistant."
},
new RequestMessage()
{
Role = "user",
Content = "Who won the world series in 2020?"
}
};
string requestData = JsonSerializer.Serialize(request);
StringContent content = new StringContent(requestData, Encoding.UTF8, "application/json");
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(apiUrl, content);
if (httpResponseMessage.IsSuccessStatusCode)
{
string responseString = await httpResponseMessage.Content.ReadAsStringAsync();
Response response = JsonSerializer.Deserialize<Response>(responseString);
Console.WriteLine(response.Choices[0].Message.Content);
}
else
{
Console.WriteLine($"Error: {httpResponseMessage.StatusCode} - {httpResponseMessage.ReasonPhrase}");
}
}
}
}
public class Request
{
[JsonPropertyName("model")]
public string Model { get; set; } = "gpt-3.5-turbo";
[JsonPropertyName("max_tokens")]
public int MaxTokens { get; set; } = 4000;
[JsonPropertyName("messages")]
public RequestMessage[] Messages { get; set; }
}
public class RequestMessage
{
[JsonPropertyName("role")]
public string Role { get; set; }
[JsonPropertyName("content")]
public string Content { get; set; }
}
public class Response
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("created")]
public int Created { get; set; }
[JsonPropertyName("model")]
public string Model { get; set; }
[JsonPropertyName("usage")]
public ResponseUsage Usage { get; set; }
[JsonPropertyName("choices")]
public ResponseChoice[] Choices { get; set; }
}
public class ResponseUsage
{
[JsonPropertyName("prompt_tokens")]
public int PromptTokens { get; set; }
[JsonPropertyName("completion_tokens")]
public int CompletionTokens { get; set; }
[JsonPropertyName("total_tokens")]
public int TotalTokens { get; set; }
}
public class ResponseChoice
{
[JsonPropertyName("message")]
public ResponseMessage Message { get; set; }
[JsonPropertyName("finish_reason")]
public string FinishReason { get; set; }
[JsonPropertyName("index")]
public int Index { get; set; }
}
public class ResponseMessage
{
[JsonPropertyName("role")]
public string Role { get; set; }
[JsonPropertyName("content")]
public string Content { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment