1- #pragma warning disable SKEXP0070
1+ #pragma warning disable SKEXP0070 , SKEXP0010
22
33using ABook . Core . Interfaces ;
44using ABook . Core . Models ;
55using Microsoft . SemanticKernel ;
66using Microsoft . SemanticKernel . ChatCompletion ;
77using Microsoft . SemanticKernel . Connectors . Ollama ;
88using Microsoft . SemanticKernel . Embeddings ;
9+ using OpenAI ;
10+ using System . ClientModel ;
911
1012namespace ABook . Infrastructure . Llm ;
1113
@@ -18,8 +20,7 @@ public IChatCompletionService CreateChatCompletion(LlmConfiguration config) =>
1820 LlmProvider . OpenAI => new Microsoft . SemanticKernel . Connectors . OpenAI . OpenAIChatCompletionService (
1921 config . ModelName , config . ApiKey ?? throw new InvalidOperationException ( "OpenAI requires an API key." ) ) ,
2022 LlmProvider . LMStudio => new Microsoft . SemanticKernel . Connectors . OpenAI . OpenAIChatCompletionService (
21- config . ModelName , config . ApiKey ?? "lm-studio" ,
22- endpoint : new Uri ( config . Endpoint . TrimEnd ( '/' ) + "/v1" ) ) ,
23+ config . ModelName , new Uri ( config . Endpoint . TrimEnd ( '/' ) + "/v1" ) , config . ApiKey ?? "lm-studio" ) ,
2324 _ => throw new NotSupportedException ( $ "Provider { config . Provider } is not yet supported.")
2425 } ;
2526
@@ -32,8 +33,9 @@ public ITextEmbeddingGenerationService CreateEmbeddingGeneration(LlmConfiguratio
3233 LlmProvider . OpenAI => new Microsoft . SemanticKernel . Connectors . OpenAI . OpenAITextEmbeddingGenerationService (
3334 embeddingModel , config . ApiKey ?? throw new InvalidOperationException ( "OpenAI requires an API key." ) ) ,
3435 LlmProvider . LMStudio => new Microsoft . SemanticKernel . Connectors . OpenAI . OpenAITextEmbeddingGenerationService (
35- embeddingModel , config . ApiKey ?? "lm-studio" ,
36- endpoint : new Uri ( config . Endpoint . TrimEnd ( '/' ) + "/v1" ) ) ,
36+ embeddingModel , new OpenAIClient (
37+ new ApiKeyCredential ( config . ApiKey ?? "lm-studio" ) ,
38+ new OpenAIClientOptions { Endpoint = new Uri ( config . Endpoint . TrimEnd ( '/' ) + "/v1" ) } ) ) ,
3739 _ => throw new NotSupportedException ( $ "Provider { config . Provider } is not yet supported for embeddings.")
3840 } ;
3941 }
@@ -57,9 +59,14 @@ public Kernel CreateKernel(LlmConfiguration config)
5759 case LlmProvider . LMStudio :
5860 var lmKey = config . ApiKey ?? "lm-studio" ;
5961 var lmEndpoint = new Uri ( config . Endpoint . TrimEnd ( '/' ) + "/v1" ) ;
60- builder . AddOpenAIChatCompletion ( config . ModelName , lmKey , endpoint : lmEndpoint ) ;
62+ builder . AddOpenAIChatCompletion ( config . ModelName , lmEndpoint , lmKey ) ;
6163 if ( ! string . IsNullOrWhiteSpace ( config . EmbeddingModelName ) )
62- builder . AddOpenAITextEmbeddingGeneration ( config . EmbeddingModelName , lmKey , endpoint : lmEndpoint ) ;
64+ {
65+ var lmClient = new OpenAIClient (
66+ new ApiKeyCredential ( lmKey ) ,
67+ new OpenAIClientOptions { Endpoint = lmEndpoint } ) ;
68+ builder . AddOpenAITextEmbeddingGeneration ( config . EmbeddingModelName , lmClient ) ;
69+ }
6370 break ;
6471 default :
6572 throw new NotSupportedException ( $ "Provider { config . Provider } is not yet supported.") ;
0 commit comments