AssistantsWithVision

 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using var api = GetAuthenticatedClient();

ImagesResponse appleImage = await api.Images.CreateImageAsync(
    prompt: "picture of apple",
    responseFormat: CreateImageRequestResponseFormat.B64Json);
byte[] appleBytes = appleImage.Data[0].Bytes;

FileInfo appleFileInfo = new($"{Guid.NewGuid()}.png");

await File.WriteAllBytesAsync(appleFileInfo.FullName, appleBytes);

Console.WriteLine($"Apple image available at:\n{new Uri(appleFileInfo.FullName).AbsoluteUri}");

Console.WriteLine($"Orange image available at:\n{new Uri("https://raw.githubusercontent.com/tryAGI/OpenAI/d237b700b03fe9913a6ff53fa623041e87705f2f/assets/orange.png")}");

OpenAIFile pictureOfAppleFile = await api.Files.CreateFileAsync(
    file: appleBytes,
    filename: appleFileInfo.Name,
    purpose: CreateFileRequestPurpose.Vision);

AssistantObject assistant = await api.Assistants.CreateAssistantAsync(
    model: CreateAssistantRequestModel.Gpt4o,
    instructions: "When asked a question, attempt to answer very concisely. " +
                  "Prefer one-sentence answers whenever feasible.");

ThreadObject thread = await api.Assistants.CreateThreadAsync(new CreateThreadRequest
{
    Messages = [
        "Hello, assistant! Please compare these two images for me:",
        pictureOfAppleFile,
        new Uri("https://raw.githubusercontent.com/tryAGI/OpenAI/d237b700b03fe9913a6ff53fa623041e87705f2f/assets/orange.png"),
    ]
});

var streamingUpdates = api.Assistants.CreateRunAsStreamAsync(
    threadId: thread.Id,
    assistantId: assistant.Id,
    instructions: "When possible, try to sneak in puns if you're asked to compare things.");

await foreach (AssistantStreamEvent streamingUpdate in streamingUpdates)
{
    if (streamingUpdate.Error is {} error)
    {
        Console.WriteLine("--- Error ---");
        Console.WriteLine($"Message: {error.Data.Message}");
        Console.WriteLine($"Code: {error.Data.Code}");
        Console.WriteLine($"Type: {error.Data.Type}");
    }
    if (streamingUpdate.ThreadRunCreated is not null)
    {
        Console.WriteLine("--- Run created! ---");
    }
    if (streamingUpdate.ThreadMessageDelta is {} delta)
    {
        foreach (var deltaVariation in delta.Data.Delta.Content ?? [])
        {
            if (deltaVariation.ImageFile is {} imageFile)
            {
                Console.WriteLine();
                Console.WriteLine(imageFile.ImageFile?.FileId);
            }
            if (deltaVariation.Text is {} text)
            {
                Console.Write(text.Text?.Value);
            }
            if (deltaVariation.Refusal is {} refusal)
            {
                Console.WriteLine();
                Console.WriteLine(refusal.Refusal);
            }
            if (deltaVariation.ImageUrl is {} imageUrl)
            {
                Console.WriteLine();
                Console.WriteLine(imageUrl.ImageUrl?.Url);
            }
        }
    }
}

_ = await api.Files.DeleteFileAsync(pictureOfAppleFile.Id);
_ = await api.Assistants.DeleteThreadAsync(thread.Id);
_ = await api.Assistants.DeleteAssistantAsync(assistant.Id);