|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace OllamaStudy.UseExtensionsAI
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// OpenAI的API 测试
|
|
|
/// Ollama兼容OpenAI接口,可以直接使用OpenAI的SDK调用
|
|
|
/// </summary>
|
|
|
public class OpenAIAPITest
|
|
|
{
|
|
|
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;
|
|
|
_ollamaOptionsMonitor = ollamaOptionsMonitor;
|
|
|
_httpClientFactory = httpClientFactory;
|
|
|
|
|
|
_defaultHttpClient = _httpClientFactory.CreateClient("OpenAIHttpClient");
|
|
|
_ollamaHttpClient = _httpClientFactory.CreateClient("OllamaHttpClient");
|
|
|
_uiUiAPIHttpClient = _httpClientFactory.CreateClient("UiUiAPIHttpClient");
|
|
|
_bailianHttpClient = _httpClientFactory.CreateClient("BailianHttpClient");
|
|
|
_zipuHttpClient = _httpClientFactory.CreateClient("ZiPuHttpClient");
|
|
|
}
|
|
|
|
|
|
#region 各种业务Client
|
|
|
/// <summary>
|
|
|
/// 从OpenAIClient获取各种业务 Client
|
|
|
/// </summary>
|
|
|
[Fact]
|
|
|
public void GetClients_Test()
|
|
|
{
|
|
|
Assert.NotNull(_defaultHttpClient);
|
|
|
Assert.NotNull(_ollamaHttpClient);
|
|
|
Assert.NotNull(_uiUiAPIHttpClient);
|
|
|
Assert.NotNull(_bailianHttpClient);
|
|
|
Assert.NotNull(_zipuHttpClient);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 音频
|
|
|
[Fact]
|
|
|
public async Task Audio_Test()
|
|
|
{
|
|
|
var requetData = new
|
|
|
{
|
|
|
//语音模型
|
|
|
model = "gpt-4o-mini-tts",
|
|
|
|
|
|
//要生成音频的文本。最大长度为4096个字符。
|
|
|
input = "你好,上海今天的天气非常好,很适合户外游玩!",
|
|
|
|
|
|
//生成音频时使用的语音。支持的语音有:alloy、echo、fable、onyx、nova 和 shimmer。
|
|
|
voice = "alloy",
|
|
|
|
|
|
//默认为 mp3 音频的格式。支持的格式有:mp3、opus、aac 和 flac。
|
|
|
response_format = "mp3",
|
|
|
|
|
|
//默认为 1 生成的音频速度。选择0.25到4.0之间的值。1.0是默认值。
|
|
|
speed = 1.0f,
|
|
|
};
|
|
|
|
|
|
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://sg.uiuiapi.com/v1/audio/speech")
|
|
|
{
|
|
|
Content = JsonContent.Create(requetData)
|
|
|
};
|
|
|
|
|
|
var responseMessage = await _uiUiAPIHttpClient.SendAsync(requestMessage);
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
|
|
|
//处理响应
|
|
|
var responseObject = await responseMessage.Content.ReadAsByteArrayAsync();
|
|
|
|
|
|
using FileStream stream = File.OpenWrite($"{Guid.NewGuid()}.mp3");
|
|
|
stream.Write(responseObject);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 聊天
|
|
|
#endregion
|
|
|
|
|
|
#region 自动补全
|
|
|
#endregion
|
|
|
|
|
|
#region 嵌入
|
|
|
#endregion
|
|
|
|
|
|
#region 微调
|
|
|
#endregion
|
|
|
|
|
|
#region 文件
|
|
|
#endregion
|
|
|
|
|
|
#region 图像
|
|
|
#endregion
|
|
|
|
|
|
#region 模型
|
|
|
|
|
|
/// <summary>
|
|
|
/// 列出模型 测试
|
|
|
/// </summary>
|
|
|
[Fact]
|
|
|
public void List_Models_Test()
|
|
|
{
|
|
|
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 审查
|
|
|
#endregion
|
|
|
|
|
|
#region 助手测试版
|
|
|
#endregion
|
|
|
|
|
|
#region 线程数
|
|
|
#endregion
|
|
|
|
|
|
#region 留言
|
|
|
#endregion
|
|
|
|
|
|
#region 运行
|
|
|
#endregion
|
|
|
|
|
|
#region 已弃用-音频
|
|
|
#endregion
|
|
|
}
|
|
|
}
|