Repository navigation
Expand file tree
/
Copy pathVectorService.cs
More file actions
59 lines (49 loc) · 1.81 KB
/
Copy pathVectorService.cs
File metadata and controls
59 lines (49 loc) · 1.81 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
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace CodingSahayi;
public static class VectorService
{
private static readonly HttpClient _httpClient = new HttpClient();
public static async Task<float[]> GetEmbeddingAsync(string text, string baseUrl = "http://localhost:11434")
{
try
{
var payload = new
{
model = "nomic-embed-text",
prompt = text
};
var content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync($"{baseUrl}/api/embeddings", content);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(responseBody);
if (doc.RootElement.TryGetProperty("embedding", out var embeddingProp))
{
return embeddingProp.EnumerateArray().Select(e => (float)e.GetDouble()).ToArray();
}
}
catch { }
return Array.Empty<float>();
}
public static float CosineSimilarity(float[] vectorA, float[] vectorB)
{
if (vectorA.Length == 0 || vectorB.Length == 0 || vectorA.Length != vectorB.Length)
return 0f;
float dotProduct = 0f;
float normA = 0f;
float normB = 0f;
for (int i = 0; i < vectorA.Length; i++)
{
dotProduct += vectorA[i] * vectorB[i];
normA += vectorA[i] * vectorA[i];
normB += vectorB[i] * vectorB[i];
}
if (normA == 0 || normB == 0) return 0f;
return dotProduct / (float)(Math.Sqrt(normA) * Math.Sqrt(normB));
}
}