添加项目文件。

This commit is contained in:
yangwx
2025-03-12 20:02:52 +08:00
parent 4d34907fa7
commit 3ccd6d9a39
32 changed files with 1833 additions and 0 deletions

14
Services/IChatService.cs Normal file
View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YwxApp.AiChat.Services
{
public interface IChatService
{
Task<string> GetResponseAsync(string prompt);
void Configure(string apiKey, string modelName = "gpt-3.5-turbo");
}
}

95
Services/OllamaService.cs Normal file
View File

@@ -0,0 +1,95 @@
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Windows;
using static YwxApp.AiChat.Services.OpenAIService;
namespace YwxApp.AiChat.Services
{
public class OllamaService : IChatService
{
private readonly HttpClient _httpClient;
private string _modelName;
public OllamaService()
{
_httpClient = new HttpClient();
}
public void Configure(string apiKey, string modelName)
{
//_httpClient.BaseAddress = new Uri(apiKey);
_modelName = modelName;
}
public async Task<string> GetResponseAsync(string prompt)
{
//var request = new
//{
// model = "deepseek-r1:1.5b",
// prompt = prompt,
// stream = false
//};
// var response = await _httpClient.PostAsJsonAsync("http://192.168.1.3:11434/api/generate", request);
// var result = await response.Content.ReadFromJsonAsync<OllamaResponse>();
// return result?.Response ?? "No response";
var request = new ChatRequest
{
Messages ={
new Message { Role = "user", Content = "你是谁" }
}
};
string json = JsonSerializer.Serialize(request, new JsonSerializerOptions
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
});
var response = await _httpClient.PostAsJsonAsync("http://192.168.1.3:11434/api/chat", request);
// var response = await _httpClient.PostAsJsonAsync("http://192.168.1.3:11434/api/generate", request);
// var result = await response.Content.ReadFromJsonAsync<OllamaResponse>();
var result = await response.Content.ReadAsStringAsync();
MessageBox.Show(result);
return "No response";
}
public class ChatRequest
{
[JsonPropertyName("messages")]
public List<Message> Messages { get; set; } = new List<Message>();
[JsonPropertyName("model")]
public string? Model { get; set; } = "deepseek-r1:1.5b"; // 默认值
[JsonPropertyName("stream")]
public bool Stream { get; set; } = false;
[JsonPropertyName("temperature")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public double Temperature { get; set; } = 0.01;
[JsonPropertyName("max_tokens")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public int MaxTokens { get; set; } = 1024;
}
public class Message
{
[JsonPropertyName("role")]
public string Role { get; set; } = "user";
[JsonPropertyName("content")]
public string Content { get; set; } = string.Empty;
}
private class OllamaResponse
{
public string Response { get; set; }
}
}
}

54
Services/OpenAIService.cs Normal file
View File

@@ -0,0 +1,54 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
namespace YwxApp.AiChat.Services
{
public class OpenAIService : IChatService
{
private readonly HttpClient _httpClient;
private string _modelName = "gpt-3.5-turbo";
public OpenAIService()
{
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri("https://api.openai.com/v1/");
}
public void Configure(string apiKey, string modelName = "gpt-3.5-turbo")
{
_httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", apiKey);
_modelName = modelName;
}
public async Task<string> GetResponseAsync(string prompt)
{
var requestBody = new
{
model = _modelName,
messages = new[] { new { role = "user", content = prompt } }
};
var response = await _httpClient.PostAsJsonAsync("chat/completions", requestBody);
var responseContent = await response.Content.ReadFromJsonAsync<OpenAIResponse>();
return responseContent?.Choices[0].Message.Content ?? "No response";
}
public class Message
{
public string Content { get; set; }
}
public class Choice
{
public Message Message { get; set; }
}
private class OpenAIResponse
{
public Choice[] Choices { get; set; }
}
}
}