-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBackendSchema.cs
More file actions
80 lines (74 loc) · 2.38 KB
/
BackendSchema.cs
File metadata and controls
80 lines (74 loc) · 2.38 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.IO;
using SwarmUI.Utils;
using Newtonsoft.Json.Linq;
using System.Net.Http;
namespace SwarmUI.Extensions.OllamaVision
{
public static class BackendSchema
{
private class ContentItem
{
public string type { get; set; }
public string text { get; set; }
public string image { get; set; }
}
private class Message
{
public string role { get; set; }
public ContentItem[] content { get; set; }
}
public static object GetVisionRequest(string imagePath, string prompt, string model)
{
if (string.IsNullOrEmpty(imagePath))
{
throw new ArgumentException("Image path cannot be null or empty.", nameof(imagePath));
}
try
{
// Read and convert image in one step
var content = new ContentItem[]
{
new ContentItem { type = "text", text = prompt },
new ContentItem
{
type = "image",
image = Convert.ToBase64String(File.ReadAllBytes(imagePath))
}
};
return new
{
model, // C# shorthand for model = model
messages = new Message[]
{
new Message
{
role = "user",
content = content
}
},
stream = false
};
}
catch (Exception ex)
{
Logs.Error($"Error creating vision request: {ex.Message}");
throw;
}
}
public static string DeserializeResponse(HttpResponseMessage response)
{
try
{
var responseContent = response.Content.ReadAsStringAsync().Result;
Logs.Debug($"Raw response: {responseContent}");
return JObject.Parse(responseContent)["message"]?["content"]?.ToString();
}
catch (Exception ex)
{
Logs.Error($"Error deserializing response: {ex.Message}");
return null;
}
}
}
}