Created
May 1, 2024 19:13
-
-
Save Dominent/07a2e3f4c09220d73122d7f6b3c4e767 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using static LangChain.Chains.Chain; | |
using LangChain.Providers.OpenAI; | |
namespace XX.Backend.Services | |
{ | |
public class OpenAILanguageModelService : ILanguageModelService | |
{ | |
private readonly ILogger<OpenAILanguageModelService> _logger; | |
private readonly IConfiguration _configuration; | |
public OpenAILanguageModelService(IConfiguration configuration, ILogger<OpenAILanguageModelService> logger) | |
{ | |
_configuration = configuration; | |
_logger = logger; | |
} | |
public async Task<string> Run(string prompt) | |
{ | |
try | |
{ | |
_logger.LogInformation("Request received with prompt: {Prompt}", prompt); | |
var model = new OpenAiChatModel(_configuration["OpenAI:ApiKey"], _configuration["OpenAI:ModelId"]); | |
var chain = Set(prompt) | LLM(model); | |
var response = await chain.Run(); | |
return response.ToString(); | |
} | |
catch (Exception ex) | |
{ | |
_logger.LogError(ex, "Error running the language model with prompt: {Prompt}", prompt); | |
return "An error occurred while processing your request."; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment