refactor for better support of streaming and etc ...

This commit is contained in:
2026-09-26 10:46:14 +03:30
parent 9afa413c6f
commit ed8a66f432
6 changed files with 132 additions and 136 deletions
+12 -43
View File
@@ -47,7 +47,7 @@ namespace xAiApi.Providers
/// <param name="prompt"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<string> AskAsync(
public virtual async Task<string> AskTextAsync(
string prompt,
CancellationToken cancellationToken = default
)
@@ -81,10 +81,6 @@ namespace xAiApi.Providers
// Retrieve Response Text ...
var result = response.Text;
//
// Dispose Client ...
client.Dispose();
//
return result;
}
@@ -95,7 +91,7 @@ namespace xAiApi.Providers
/// <param name="prompt"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async IAsyncEnumerable<string> AskAsEnumerable(
public virtual async IAsyncEnumerable<string> AskTextAsEnumerable(
string prompt,
[EnumeratorCancellation]
CancellationToken cancellationToken = default
@@ -117,7 +113,6 @@ namespace xAiApi.Providers
);
//
var result = "";
await foreach (var res in enumerable)
{
//
@@ -128,15 +123,8 @@ namespace xAiApi.Providers
}
//
result += res.Text;
yield return res.Text;
}
//
// Dispose Client ...
client.Dispose();
//
yield return result;
}
/// <summary>
@@ -172,6 +160,7 @@ namespace xAiApi.Providers
var ollamaClient = new OllamaApiClient(httpClient, model);
result =
new ChatClientBuilder(ollamaClient)
.UseFunctionInvocation()
.Build();
break;
@@ -188,9 +177,12 @@ namespace xAiApi.Providers
}
);
result =
openAiClient
.GetChatClient(model)
.AsIChatClient();
new ChatClientBuilder(
openAiClient
.GetChatClient(model)
.AsIChatClient())
.UseFunctionInvocation()
.Build();
break;
//
@@ -206,29 +198,6 @@ namespace xAiApi.Providers
break;
}
// //
// if (isOllamaBased)
// {
// }
// else if (isDeepSeekBased)
// {
// //
// if (apiKey is null ||
// string.IsNullOrWhiteSpace(apiKey))
// {
// throw new Exception("Inavlid Args ...");
// }
// //
// var deepseekClient = new DeepSeekChatClient(apiKey: apiKey);
// result = deepseekClient;
// }
// else
// {
// //
// // TODO: Implement this ...
// }
//
if (result.IsNullOrDefault())
{
@@ -330,7 +299,7 @@ namespace xAiApi.Providers
// Preparing Introduction Prompt ...
var introductionPrompt = new ChatMessage(
ChatRole.System,
configuration.GetPrompt()
configuration.GetPrompt(XAiApiConstants.XAiApiIntroductionPromptName)
);
//
@@ -339,7 +308,7 @@ namespace xAiApi.Providers
introductionPrompt,
];
if (forceMessage &&
message is not null)
!message.IsNullOrDefault())
{
result.Add(message);
}
+17 -82
View File
@@ -37,11 +37,7 @@ namespace xAiApi.Providers
XAiApiConfiguration configuration,
ILogger<XAiEmbeddingServiceBase> logger,
string embeddingModelName = XAiApiConstants.XAiEmbeddingModelName
) : base(
dataProvider,
logger,
configuration
)
) : base(dataProvider, logger, configuration)
{
//
// Prepare Embedding Model Descriptor ...
@@ -75,15 +71,13 @@ namespace xAiApi.Providers
//
using var generator = GetEmbeddingGenerator();
//
GeneratedEmbeddings<Embedding<float>> response = null;
try
{
response = await generator.GenerateAsync([content]);
}
catch (Exception ex)
{
logger.LogError(ex.Message);
}
response = await generator.GenerateAsync(
values: [content],
cancellationToken: cancellationToken
);
//
if (response.IsNullOrDefault())
@@ -103,40 +97,6 @@ namespace xAiApi.Providers
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 ...
VectorHelper.Normalize(result);
@@ -166,41 +126,25 @@ namespace xAiApi.Providers
}
//
// Retrieve Ollama Client ...
using var client = GetEmbeddingClient();
using var generator = GetEmbeddingGenerator();
//
// Create Embedding Request ....
var request = new EmbedRequest
{
Input = [.. batch],
Model = descriptor.LLM,
};
//
// Make Request and Recieve Response ...
var response = await client.EmbedAsync(
request: request,
GeneratedEmbeddings<Embedding<float>> response = null;
response = await generator.GenerateAsync(
values: [.. batch],
cancellationToken: cancellationToken
);
//
if (response.IsNullOrDefault())
{
XException.ActionFailed.Throw();
}
//
// Generae Result ...
var result = response?
.Embeddings
.ToArray()
?? [];
//
// Validate Result ...
if (result.Length == 0)
{
XException.ActionFailed.Throw();
}
var result = response
.Select(e => e.Vector.ToArray())
.ToList();
//
// Normalize ...
@@ -214,18 +158,9 @@ namespace xAiApi.Providers
}
/// <summary>
/// Access Embedding Client ...
/// Embedding Generator ...
/// </summary>
/// <returns></returns>
public OllamaApiClient GetEmbeddingClient()
{
//
var result = new OllamaApiClient(new Uri(descriptor.Url));
//
return result;
}
public IEmbeddingGenerator<string, Embedding<float>> GetEmbeddingGenerator()
{
//