adding vector model enabling and also test all using OpenAI model ...
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.AI;
|
||||||
using OllamaSharp;
|
using OllamaSharp;
|
||||||
|
|
||||||
namespace xAiApi.Interfaces
|
namespace xAiApi.Interfaces
|
||||||
@@ -37,5 +38,7 @@ namespace xAiApi.Interfaces
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
OllamaApiClient GetEmbeddingClient();
|
OllamaApiClient GetEmbeddingClient();
|
||||||
|
|
||||||
|
IEmbeddingGenerator<string, Embedding<float>> GetEmbeddingGenerator();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -179,7 +179,7 @@ namespace xAiApi.Providers
|
|||||||
case XAiModelProviderType.OpenAI:
|
case XAiModelProviderType.OpenAI:
|
||||||
//
|
//
|
||||||
// Instance Ollama Client using Custom HttpCLient ...
|
// Instance Ollama Client using Custom HttpCLient ...
|
||||||
var llamaClient = new OpenAIClient(
|
var openAiClient = new OpenAIClient(
|
||||||
new ApiKeyCredential(apiKey.IsNullOrEmpty() ? XAiApiConstants.XOpenAINoKey : apiKey),
|
new ApiKeyCredential(apiKey.IsNullOrEmpty() ? XAiApiConstants.XOpenAINoKey : apiKey),
|
||||||
new OpenAIClientOptions
|
new OpenAIClientOptions
|
||||||
{
|
{
|
||||||
@@ -188,7 +188,7 @@ namespace xAiApi.Providers
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
result =
|
result =
|
||||||
llamaClient
|
openAiClient
|
||||||
.GetChatClient(model)
|
.GetChatClient(model)
|
||||||
.AsIChatClient();
|
.AsIChatClient();
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -16,6 +16,11 @@ using xExceptions.Constants;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.AI;
|
||||||
|
using xAiModels.Constants;
|
||||||
|
using OpenAI;
|
||||||
|
using System.ClientModel;
|
||||||
|
using System.ClientModel.Primitives;
|
||||||
|
|
||||||
namespace xAiApi.Providers
|
namespace xAiApi.Providers
|
||||||
{
|
{
|
||||||
@@ -25,6 +30,7 @@ namespace xAiApi.Providers
|
|||||||
public abstract class XAiEmbeddingServiceBase : XAIServiceBase, IXAiEmbeddingServiceBase
|
public abstract class XAiEmbeddingServiceBase : XAIServiceBase, IXAiEmbeddingServiceBase
|
||||||
{
|
{
|
||||||
private readonly XAiModelDescriptor descriptor;
|
private readonly XAiModelDescriptor descriptor;
|
||||||
|
private readonly ILogger<XAiEmbeddingServiceBase> logger;
|
||||||
|
|
||||||
protected XAiEmbeddingServiceBase(
|
protected XAiEmbeddingServiceBase(
|
||||||
IXAiDataProvider dataProvider,
|
IXAiDataProvider dataProvider,
|
||||||
@@ -44,6 +50,9 @@ namespace xAiApi.Providers
|
|||||||
{
|
{
|
||||||
XException.InvalidConfiguration.Throw();
|
XException.InvalidConfiguration.Throw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -65,23 +74,18 @@ namespace xAiApi.Providers
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Retrieve Ollama Client ...
|
using var generator = GetEmbeddingGenerator();
|
||||||
using var client = GetEmbeddingClient();
|
GeneratedEmbeddings<Embedding<float>> response = null;
|
||||||
|
try
|
||||||
//
|
|
||||||
// Create Embedding Request ....
|
|
||||||
var request = new EmbedRequest
|
|
||||||
{
|
{
|
||||||
Input = [content],
|
response = await generator.GenerateAsync([content]);
|
||||||
Model = descriptor.LLM,
|
}
|
||||||
};
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogError(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Make Request and Recieve Response ...
|
|
||||||
var response = await client.EmbedAsync(
|
|
||||||
request: request,
|
|
||||||
cancellationToken: cancellationToken
|
|
||||||
);
|
|
||||||
if (response.IsNullOrDefault())
|
if (response.IsNullOrDefault())
|
||||||
{
|
{
|
||||||
XException.ActionFailed.Throw();
|
XException.ActionFailed.Throw();
|
||||||
@@ -89,15 +93,50 @@ namespace xAiApi.Providers
|
|||||||
|
|
||||||
//
|
//
|
||||||
// Generae Result ...
|
// Generae Result ...
|
||||||
var result = response?
|
var result = response
|
||||||
.Embeddings?
|
.First()?
|
||||||
.FirstOrDefault()
|
.Vector
|
||||||
|
.ToArray()
|
||||||
?? [];
|
?? [];
|
||||||
if (result.Length == 0)
|
if (result.Length == 0)
|
||||||
{
|
{
|
||||||
XException.ActionFailed.Throw();
|
XException.ActionFailed.Throw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Retrieve Ollama Client ...
|
||||||
|
// using var client = GetEmbeddingClient();
|
||||||
|
|
||||||
|
// //
|
||||||
|
// // Create Embedding Request ....
|
||||||
|
// var request = new EmbedRequest
|
||||||
|
// {
|
||||||
|
// Input = [content],
|
||||||
|
// Model = descriptor.LLM,
|
||||||
|
// };
|
||||||
|
|
||||||
|
// //
|
||||||
|
// // Make Request and Recieve Response ...
|
||||||
|
// var response = await client.EmbedAsync(
|
||||||
|
// request: request,
|
||||||
|
// cancellationToken: cancellationToken
|
||||||
|
// );
|
||||||
|
// if (response.IsNullOrDefault())
|
||||||
|
// {
|
||||||
|
// XException.ActionFailed.Throw();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //
|
||||||
|
// // Generae Result ...
|
||||||
|
// var result = response?
|
||||||
|
// .Embeddings?
|
||||||
|
// .FirstOrDefault()
|
||||||
|
// ?? [];
|
||||||
|
// if (result.Length == 0)
|
||||||
|
// {
|
||||||
|
// XException.ActionFailed.Throw();
|
||||||
|
// }
|
||||||
|
|
||||||
//
|
//
|
||||||
// Normalize ...
|
// Normalize ...
|
||||||
VectorHelper.Normalize(result);
|
VectorHelper.Normalize(result);
|
||||||
@@ -186,5 +225,53 @@ namespace xAiApi.Providers
|
|||||||
//
|
//
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEmbeddingGenerator<string, Embedding<float>> GetEmbeddingGenerator()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
IEmbeddingGenerator<string, Embedding<float>> result = null;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!descriptor.IsValid())
|
||||||
|
{
|
||||||
|
XException.InvalidData.Throw("Invalid Embedding Model ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
switch (descriptor.Provider)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
case XAiModelProviderType.OpenAI:
|
||||||
|
//
|
||||||
|
var openAiClient = new OpenAIClient(
|
||||||
|
new ApiKeyCredential(descriptor.ApiKey.IsNullOrEmpty() ? XAiApiConstants.XOpenAINoKey : descriptor.ApiKey),
|
||||||
|
new OpenAIClientOptions
|
||||||
|
{
|
||||||
|
Endpoint = new Uri(descriptor.Url),
|
||||||
|
Transport = new HttpClientPipelineTransport(GetHttpClient(descriptor.Url))
|
||||||
|
}
|
||||||
|
);
|
||||||
|
result = openAiClient
|
||||||
|
.GetEmbeddingClient(descriptor.LLM)
|
||||||
|
.AsIEmbeddingGenerator();
|
||||||
|
break;
|
||||||
|
|
||||||
|
//
|
||||||
|
case XAiModelProviderType.Ollama:
|
||||||
|
//
|
||||||
|
var ollamaClient = new OllamaApiClient(new Uri(descriptor.Url));
|
||||||
|
result = ollamaClient;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (result.IsNull())
|
||||||
|
{
|
||||||
|
XException.InvalidData.Throw("Invalid Embedding Model ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,14 +42,14 @@
|
|||||||
"Name": "Gemma",
|
"Name": "Gemma",
|
||||||
"LLM": "gemma3:1b",
|
"LLM": "gemma3:1b",
|
||||||
"Provider": "OpenAI",
|
"Provider": "OpenAI",
|
||||||
"Url": "http://178.131.88.93:1112"
|
"Url": "http://178.131.88.93:1112/v1"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ApiKey": "",
|
"ApiKey": "",
|
||||||
"Name": "Embed",
|
"Name": "Embed",
|
||||||
"LLM": "bge-m3",
|
"LLM": "bge-m3",
|
||||||
"Provider": "OpenAI",
|
"Provider": "OpenAI",
|
||||||
"Url": "http://178.131.88.93:1113"
|
"Url": "http://178.131.88.93:1113/v1"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"Prompts": [
|
"Prompts": [
|
||||||
|
|||||||
Reference in New Issue
Block a user