using System.Net.Http.Headers; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using static Google.Apis.Requests.BatchRequest; namespace AiAgent.Core.Infrastructure { // This script demonstrates how to query the Deepseek Chat API using C#. // It uses the built-in HttpClient for making HTTP requests and System.Text.Json for handling JSON. public static class DeepseekApiClient { private static string apiKey => "sk-7223f4a765054318a06330d8db450d3f"; private static readonly HttpClient client = new HttpClient(); private const string ApiBaseUrl = "https://api.deepseek.com/v1/"; /// /// Sends a prompt to the Deepseek API and returns the response. /// /// The user's prompt to send to the model. /// The content of the model's response as a string, or an error message. public static async Task QueryDeepseekApiAsyncQuery(string prompt, string tag, string modelName = null, List inputDataFiles = null) { try { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); var requestBody = new ChatRequest { Model = modelName is null ? "deepseek-chat" : modelName, Messages = new List { new MessageDS { Role = "system", Content = "You are serious news editor in charge of reviewing your colleagues news articles." }, new MessageDS { Role = "user", Content = prompt } }, Stream = false }; var jsonRequest = JsonSerializer.Serialize(requestBody); var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json"); var response = await client.PostAsync($"{ApiBaseUrl}chat/completions", content); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var chatResponse = JsonSerializer.Deserialize(jsonResponse); Console.WriteLine($"Deepseek API response:\n{jsonResponse}"); return chatResponse?.Choices?[0]?.Message?.Content ?? "No content received."; } else { var errorContent = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Error Deepseek API response:\n {response.StatusCode} - {errorContent}"); return $"Error: {response.StatusCode} - {errorContent}"; } } catch (Exception e) { return $"An unexpected error occurred: {e.Message}"; } } //public static async Task Main(string[] args) //{ // // Example usage of the function // string userPrompt = "Hello, what can you do?"; // Console.WriteLine($"User: {userPrompt}"); // // Get the response from the API // string apiResponse = await QueryDeepseekApiAsync(userPrompt); // Console.WriteLine($"Deepseek: {apiResponse}"); // // Another example // string userPrompt2 = "Write a short poem about coding in C#."; // Console.WriteLine($"\nUser: {userPrompt2}"); // string apiResponse2 = await QueryDeepseekApiAsync(userPrompt2); // Console.WriteLine($"Deepseek: {apiResponse2}"); //} } // C# classes to represent the JSON structure for the request and response public class ChatRequest { [JsonPropertyName("model")] public string Model { get; set; } [JsonPropertyName("messages")] public List Messages { get; set; } [JsonPropertyName("stream")] public bool Stream { get; set; } } public class MessageDS { [JsonPropertyName("role")] public string Role { get; set; } [JsonPropertyName("content")] public string Content { get; set; } } public class ChatResponse { [JsonPropertyName("choices")] public List Choices { get; set; } } public class ChoiceDS { [JsonPropertyName("message")] public MessageDS Message { get; set; } } }