Skip to content

Commit

Permalink
Refactor MistralClient.cs to reduce code reptition.
Browse files Browse the repository at this point in the history
  • Loading branch information
markjamesm committed Dec 17, 2023
1 parent beb7250 commit 4b6c03e
Showing 1 changed file with 48 additions and 22 deletions.
70 changes: 48 additions & 22 deletions src/MistralClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,24 @@ namespace MistralSharp
/// </summary>
public class MistralClient
{
private static readonly HttpClient _httpClient = new HttpClient();
private static readonly string _baseUrl = "https://api.mistral.ai/v1";
private static readonly HttpClient HttpClient = new HttpClient();
private const string BaseUrl = "https://api.mistral.ai/v1";

public MistralClient(string apiKey)
{
_httpClient.DefaultRequestHeaders.Authorization =
HttpClient.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiKey);
}

private static async Task<string> GetResponseAsync(string endpoint, string modelType = "")
{
var returnMessage = await _httpClient.GetAsync(_baseUrl + (endpoint ?? "")).ConfigureAwait(false);

return await returnMessage.Content.ReadAsStringAsync();
}



/// <summary>
/// Enables you to chat with an AI model on the Mistral platform.
/// </summary>
/// <param name="chatRequest"></param>
/// <returns>A ChatResponse object</returns>
/// <param name="chatRequest">A ChatRequest object containing message and other data.</param>
/// <returns>A ChatResponse object with the AI's response.</returns>
public async Task<ChatResponse> ChatAsync(ChatRequest chatRequest)
{
var jsonRequest = JsonSerializer.Serialize(chatRequest);
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");

var response = await _httpClient.PostAsync(_baseUrl + "/chat/completions", content).ConfigureAwait(false);
var jsonResponse = await response.Content.ReadAsStringAsync();
var jsonResponse = await PostToApiAsync(chatRequest, "/chat/completions");
var chatResponseDto = JsonSerializer.Deserialize<ChatResponseDto>(jsonResponse);

var chatResponse = new ChatResponse()
Expand Down Expand Up @@ -73,6 +62,7 @@ public async Task<ChatResponse> ChatAsync(ChatRequest chatRequest)
return chatResponse;
}


/// <summary>
/// Get the list of available models offered on the Mistral AI platform.
/// </summary>
Expand Down Expand Up @@ -115,13 +105,17 @@ public async Task<AvailableModels> GetAvailableModelsAsync()
return availableModels;
}


/// <summary>
/// The embeddings API allows you to embed sentences and can be used to power a RAG application.
/// </summary>
/// <param name="embeddingRequest"></param>
/// <returns>An EmbeddingResponse object.</returns>
public async Task<EmbeddingResponse> CreateEmbeddingsAsync(EmbeddingRequest embeddingRequest)
{
var jsonRequest = JsonSerializer.Serialize(embeddingRequest);
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync(_baseUrl + "/embeddings", content).ConfigureAwait(false);
var jsonResponse = await response.Content.ReadAsStringAsync();
var jsonResponse = await PostToApiAsync(embeddingRequest, "/embeddings");
var embeddingResponseDto = JsonSerializer.Deserialize<EmbeddingResponseDto>(jsonResponse);

var embeddingResponse = new EmbeddingResponse()
{
Id = embeddingResponseDto.Id,
Expand All @@ -140,7 +134,39 @@ public async Task<EmbeddingResponse> CreateEmbeddingsAsync(EmbeddingRequest embe
}

};

return embeddingResponse;
}


/// <summary>
/// Retrieves the response from the specified API endpoint asynchronously.
/// </summary>
/// <param name="endpoint">The API endpoint to send the request to.</param>
/// <param name="modelType">The type of model to deserialize the response content into (optional).</param>
/// <returns>The response content as a string.</returns>
private static async Task<string> GetResponseAsync(string endpoint, string modelType = "")
{
var returnMessage = await HttpClient.GetAsync(BaseUrl + (endpoint ?? "")).ConfigureAwait(false);

return await returnMessage.Content.ReadAsStringAsync();
}


/// <summary>
/// Helper method to serialize an object to JSON and return a JSON response string.
/// </summary>
/// <param name="objectToSerialize">The object to be serialized.</param>
/// <param name="endpoint">The endpoint to send the JSON request to.</param>
/// <returns>A string representing the JSON response.</returns>
private static async Task<string> PostToApiAsync(object objectToSerialize, string endpoint)
{
var jsonRequest = JsonSerializer.Serialize(objectToSerialize);
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
var response = await HttpClient.PostAsync(BaseUrl + endpoint, content).ConfigureAwait(false);
var jsonResponse = await response.Content.ReadAsStringAsync();

return jsonResponse;
}
}
}

0 comments on commit 4b6c03e

Please sign in to comment.