pu

Buscar este blog

miércoles, 14 de agosto de 2024

conectar a claude api con subida de archivo para analizar

 using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text;
using Newtonsoft.Json;

class Program
{
    static async Task Main(string[] args)
    {
        string apiKey = "YOUR_API_KEY_HERE";
        string apiUrl = "https://api.anthropic.com/v1/messages";

        using (var httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

            var requestContent = new MultipartFormDataContent();

            // Add the prompt
            string prompt = "Your prompt text here";
            requestContent.Add(new StringContent(prompt), "prompt");

            // Attach a file
            var fileContent = new ByteArrayContent(await System.IO.File.ReadAllBytesAsync("path/to/your/file.txt"));
            fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain");
            requestContent.Add(fileContent, "file", "file.txt");

            // Set other parameters as needed
            requestContent.Add(new StringContent("claude-3.5-sonnet"), "model");

            try
            {
                var response = await httpClient.PostAsync(apiUrl, requestContent);
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Error: {e.Message}");
            }
        }
    }
}

No hay comentarios:

Publicar un comentario