|
|
|
@ -12,17 +12,32 @@ namespace OllamaStudy.UseExtensionsAI
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class OpenAIAPITest
|
|
|
|
|
{
|
|
|
|
|
private ITestOutputHelper _output;
|
|
|
|
|
private IOptionsMonitor<OllamaServerOption> _ollamaOptionsMonitor;
|
|
|
|
|
private OpenAIClient _defaultOpenAIClient;
|
|
|
|
|
private ChatClient _chatClient;
|
|
|
|
|
|
|
|
|
|
public OpenAIAPITest(ITestOutputHelper outputHelper, OpenAIClient defaultOpenAIClient, IOptionsMonitor<OllamaServerOption> ollamaOptionsMonitor)
|
|
|
|
|
private readonly ITestOutputHelper _output;
|
|
|
|
|
private readonly IOptionsMonitor<OllamaServerOption> _ollamaOptionsMonitor;
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
private readonly HttpClient _defaultHttpClient;
|
|
|
|
|
private readonly HttpClient _ollamaHttpClient;
|
|
|
|
|
private readonly HttpClient _uiUiAPIHttpClient;
|
|
|
|
|
private readonly HttpClient _bailianHttpClient;
|
|
|
|
|
private readonly HttpClient _zipuHttpClient;
|
|
|
|
|
|
|
|
|
|
public OpenAIAPITest
|
|
|
|
|
(
|
|
|
|
|
ITestOutputHelper outputHelper,
|
|
|
|
|
OpenAIClient defaultOpenAIClient,
|
|
|
|
|
IOptionsMonitor<OllamaServerOption> ollamaOptionsMonitor,
|
|
|
|
|
IHttpClientFactory httpClientFactory
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
_output = outputHelper;
|
|
|
|
|
_defaultOpenAIClient = defaultOpenAIClient;
|
|
|
|
|
_ollamaOptionsMonitor = ollamaOptionsMonitor;
|
|
|
|
|
_chatClient = _defaultOpenAIClient.GetChatClient(_ollamaOptionsMonitor.CurrentValue.Model);
|
|
|
|
|
_httpClientFactory = httpClientFactory;
|
|
|
|
|
|
|
|
|
|
_defaultHttpClient = _httpClientFactory.CreateClient("OpenAIHttpClient");
|
|
|
|
|
_ollamaHttpClient = _httpClientFactory.CreateClient("OllamaHttpClient");
|
|
|
|
|
_uiUiAPIHttpClient = _httpClientFactory.CreateClient("UiUiAPIHttpClient");
|
|
|
|
|
_bailianHttpClient = _httpClientFactory.CreateClient("BailianHttpClient");
|
|
|
|
|
_zipuHttpClient = _httpClientFactory.CreateClient("ZiPuHttpClient");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region 各种业务Client
|
|
|
|
@ -32,74 +47,52 @@ namespace OllamaStudy.UseExtensionsAI
|
|
|
|
|
[Fact]
|
|
|
|
|
public void GetClients_Test()
|
|
|
|
|
{
|
|
|
|
|
#pragma warning disable OPENAI001
|
|
|
|
|
Assert.NotNull(_defaultOpenAIClient);
|
|
|
|
|
|
|
|
|
|
//音频客户端
|
|
|
|
|
var audioClient = _defaultOpenAIClient.GetAudioClient(_ollamaOptionsMonitor.CurrentValue.Model);
|
|
|
|
|
Assert.NotNull(audioClient);
|
|
|
|
|
|
|
|
|
|
//聊天客户端
|
|
|
|
|
var chatClient = _defaultOpenAIClient.GetChatClient(_ollamaOptionsMonitor.CurrentValue.Model);
|
|
|
|
|
Assert.NotNull(chatClient);
|
|
|
|
|
|
|
|
|
|
//嵌入客户端
|
|
|
|
|
var embeddingClient = _defaultOpenAIClient.GetEmbeddingClient(_ollamaOptionsMonitor.CurrentValue.Model);
|
|
|
|
|
Assert.NotNull(embeddingClient);
|
|
|
|
|
|
|
|
|
|
//图像客户端
|
|
|
|
|
var imageClient = _defaultOpenAIClient.GetImageClient(_ollamaOptionsMonitor.CurrentValue.Model);
|
|
|
|
|
Assert.NotNull(imageClient);
|
|
|
|
|
|
|
|
|
|
//微调客户端
|
|
|
|
|
var moderationClient = _defaultOpenAIClient.GetModerationClient(_ollamaOptionsMonitor.CurrentValue.Model);
|
|
|
|
|
Assert.NotNull(moderationClient);
|
|
|
|
|
|
|
|
|
|
//文件客户端
|
|
|
|
|
var openAIFileClient = _defaultOpenAIClient.GetOpenAIFileClient();
|
|
|
|
|
Assert.NotNull(openAIFileClient);
|
|
|
|
|
Assert.NotNull(_defaultHttpClient);
|
|
|
|
|
Assert.NotNull(_ollamaHttpClient);
|
|
|
|
|
Assert.NotNull(_uiUiAPIHttpClient);
|
|
|
|
|
Assert.NotNull(_bailianHttpClient);
|
|
|
|
|
Assert.NotNull(_zipuHttpClient);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
//模型客户端
|
|
|
|
|
var modelClient = _defaultOpenAIClient.GetOpenAIModelClient();
|
|
|
|
|
Assert.NotNull(modelClient);
|
|
|
|
|
#region 音频
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Audio_Test()
|
|
|
|
|
{
|
|
|
|
|
var requetData = new
|
|
|
|
|
{
|
|
|
|
|
//语音模型
|
|
|
|
|
model = "gpt-4o-mini-tts",
|
|
|
|
|
|
|
|
|
|
//助手客户端(仅评估)
|
|
|
|
|
var assistantClient = _defaultOpenAIClient.GetAssistantClient();
|
|
|
|
|
Assert.NotNull(assistantClient);
|
|
|
|
|
//要生成音频的文本。最大长度为4096个字符。
|
|
|
|
|
input = "你好,上海今天的天气非常好,很适合户外游玩!",
|
|
|
|
|
|
|
|
|
|
//批量客户端(仅评估)
|
|
|
|
|
var batchClient = _defaultOpenAIClient.GetBatchClient();
|
|
|
|
|
Assert.NotNull(batchClient);
|
|
|
|
|
//生成音频时使用的语音。支持的语音有:alloy、echo、fable、onyx、nova 和 shimmer。
|
|
|
|
|
voice = "alloy",
|
|
|
|
|
|
|
|
|
|
//评估客户端(仅评估)
|
|
|
|
|
var evaluationClient = _defaultOpenAIClient.GetEvaluationClient();
|
|
|
|
|
Assert.NotNull(evaluationClient);
|
|
|
|
|
//默认为 mp3 音频的格式。支持的格式有:mp3、opus、aac 和 flac。
|
|
|
|
|
response_format = "mp3",
|
|
|
|
|
|
|
|
|
|
//微调客户端(仅评估)
|
|
|
|
|
var FineTuningClient = _defaultOpenAIClient.GetFineTuningClient();
|
|
|
|
|
Assert.NotNull(FineTuningClient);
|
|
|
|
|
//默认为 1 生成的音频速度。选择0.25到4.0之间的值。1.0是默认值。
|
|
|
|
|
speed = 1.0f,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//响应客户端(仅评估)
|
|
|
|
|
var openAIResponseClient = _defaultOpenAIClient.GetOpenAIResponseClient(_ollamaOptionsMonitor.CurrentValue.Model);
|
|
|
|
|
Assert.NotNull(openAIResponseClient);
|
|
|
|
|
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://sg.uiuiapi.com/v1/audio/speech")
|
|
|
|
|
{
|
|
|
|
|
Content = JsonContent.Create(requetData)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//实时客户端(仅评估)
|
|
|
|
|
#pragma warning disable OPENAI002
|
|
|
|
|
var realtimeClient = _defaultOpenAIClient.GetRealtimeClient();
|
|
|
|
|
Assert.NotNull(realtimeClient);
|
|
|
|
|
#pragma warning restore OPENAI002
|
|
|
|
|
var responseMessage = await _uiUiAPIHttpClient.SendAsync(requestMessage);
|
|
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
//向量存储客户端(仅评估)
|
|
|
|
|
var vectorStoreClient = _defaultOpenAIClient.GetVectorStoreClient();
|
|
|
|
|
Assert.NotNull(vectorStoreClient);
|
|
|
|
|
//处理响应
|
|
|
|
|
var responseObject = await responseMessage.Content.ReadAsByteArrayAsync();
|
|
|
|
|
|
|
|
|
|
#pragma warning restore OPENAI001
|
|
|
|
|
using FileStream stream = File.OpenWrite($"{Guid.NewGuid()}.mp3");
|
|
|
|
|
stream.Write(responseObject);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 音频
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 聊天
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
@ -126,11 +119,7 @@ namespace OllamaStudy.UseExtensionsAI
|
|
|
|
|
[Fact]
|
|
|
|
|
public void List_Models_Test()
|
|
|
|
|
{
|
|
|
|
|
var modelClient = _defaultOpenAIClient.GetOpenAIModelClient();
|
|
|
|
|
|
|
|
|
|
OpenAI.Models.OpenAIModelCollection openAIModelCollection = modelClient.GetModels().Value;
|
|
|
|
|
|
|
|
|
|
_output.WriteLine($"Ollama服务中,共有{openAIModelCollection.Count()}个模型,包括[{string.Join(",", openAIModelCollection)}]");
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|