Closed JWJUN233233 closed 2 weeks ago
想问一下,部署了openai_api版本后,如何通过ip访问呀
以后不要在不合适的地方问问题
openai_api默认会监听8000端口 直接访问就行 http协议 以下是c#的自动让ai描述图片的代码 需要nuget程序包
<PackageReference Include="Betalgo.OpenAI" Version="8.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
public static class Program
{
static async Task Main(string[] args)
{
int chunkSize = 300;
OpenAIService service = new(new OpenAiOptions() { ProviderType = ProviderType.OpenAi, BaseDomain = "http://localhost:8000/", ApiKey = "none" });
ModelListResponse modelList = await service.ListModel();
service.SetDefaultModelId(modelList.Models.First().Id);
Console.WriteLine(modelList.Models.First().Id);
DirectoryInfo directory = new(Path.Combine(Environment.CurrentDirectory, "images"));
List<ImageInfo> images = [];
List<FileInfo[]> chunks = directory.GetFiles().Chunk(chunkSize).ToList();
for (int i = 0; i < chunks.Count; i++)
{
foreach (FileInfo file in chunks[i])
{
if (!file.Name.EndsWith(".jpg")) continue;
Stopwatch stopwatch = new();
stopwatch.Start();
string imageData = Convert.ToBase64String(File.ReadAllBytes(file.FullName));
//聊天信息
List<ChatMessage> Messages =
[
ChatMessage.FromUser(
[
MessageContent.ImageUrlContent($"data:image/jpeg;base64,{imageData}",
ImageStatics.ImageDetailTypes.High),
MessageContent.TextContent("描述这张图片。")
])
];
ChatCompletionCreateResponse completionResult = await service.ChatCompletion.CreateCompletion(new() { Messages = Messages });
string? description = completionResult.Choices.First().Message.Content;
Console.WriteLine("================");
Console.WriteLine($"{file.Name}\n{completionResult.Choices.First().Message.Content}");
images.Add(new ImageInfo() { Name = file.Name, Description = description });
stopwatch.Stop();
Console.WriteLine($"本图片用时{stopwatch.Elapsed.Seconds} s");
}
DateTime time = DateTime.Now;
Directory.CreateDirectory("save");
File.WriteAllText($"save/{(i * chunkSize).ToString("D5")}_{(i * chunkSize + chunkSize).ToString("D5")}_{time.Year}{time.Month.ToString("D2")}{time.Day.ToString("D2")}{time.Hour.ToString("D2")}{time.Minute.ToString("D2")}.json", Newtonsoft.Json.JsonConvert.SerializeObject(images));
images = [];
}
}
}
public class ImageInfo
{
public string? Name { get; set; }
public string? Description { get; set; }
}
想问一下,部署了openai_api版本后,如何通过ip访问呀