This repository was archived by the owner on Mar 30, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAIAltGen.cs
More file actions
64 lines (58 loc) · 2.21 KB
/
OpenAIAltGen.cs
File metadata and controls
64 lines (58 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using AltGen.Config;
using Microsoft.Extensions.Logging;
using OpenAI;
using OpenAI.Managers;
using OpenAI.ObjectModels;
using OpenAI.ObjectModels.RequestModels;
namespace AltGen
{
public class OpenAiAltGen
{
private const int MaxLength = 500;
private readonly Secrets _secrets;
private readonly ILogger<OpenAiAltGen> _logger;
public OpenAiAltGen(Secrets secrets, ILogger<OpenAiAltGen> logger)
{
_secrets = secrets;
_logger = logger;
}
public async Task<string?> GetImageDescription(string filePath)
{
var service = Login();
var descriptionResult = await service.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest
{
Messages = new List<ChatMessage>
{
ChatMessage.FromSystem(
$"You are an image analyzer assistant that speaks German. Never use more than {MaxLength} Characters for your reply"),
ChatMessage.FromUser(new List<MessageContent>
{
MessageContent.TextContent("Was ist in dem Bild?"),
MessageContent.ImageUrlContent(filePath)
})
},
Model = Models.Gpt_4o_mini,
Temperature = 0.2f,
MaxTokens = 400
});
if (descriptionResult.Successful)
{
var content = descriptionResult.Choices.First().Message.Content;
var cost = descriptionResult.Usage.TotalTokens;
if (content?.Length > MaxLength) content = content[..MaxLength];
_logger.LogDebug("Successfully received a description with '{Cost}' Tokens: {Content}", cost, content);
return content;
}
_logger.LogWarning("Could not receive description: '{Error}'", descriptionResult.Error?.Message);
return null;
}
private OpenAIService Login()
{
var openAiService = new OpenAIService(new OpenAiOptions
{
ApiKey = _secrets.OpenAiKey
});
return openAiService;
}
}
}