MCPcopy Create free account
hub / github.com/SensAI-PT/RAGMeUp / LLMHelper

Class LLMHelper

server/LLMHelper.py:30–277  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28 yield chunk.message.content
29
30class LLMHelper:
31 def __init__(self, logger):
32 self.logger = logger
33 self.temperature = float(os.getenv("temperature", 0.0))
34 self.client = self.initialize_client()
35
36 def initialize_client(self):
37 """Initialize the Language Model based on environment configurations."""
38 if os.getenv("use_openai") == "True":
39 self.logger.info("Initializing OpenAI conversation.")
40 api_key = os.getenv("OPENAI_API_KEY")
41 if not api_key:
42 print("Error: OPENAI_API_KEY not found in .env file.")
43 return None
44 return openai.OpenAI(api_key=api_key)
45 if os.getenv("use_gemini") == "True":
46 self.logger.info("Initializing Gemini conversation.")
47 api_key = os.getenv("GOOGLE_API_KEY")
48 if not api_key:
49 print("Error: GOOGLE_API_KEY not found in .env file.")
50 return None
51 return genai.Client(api_key=api_key)
52 if os.getenv("use_azure") == "True":
53 self.logger.info("Initializing Azure OpenAI conversation.")
54 api_key = os.getenv("AZURE_OPENAI_API_KEY")
55 api_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
56 deployment_name = os.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT_NAME")
57 api_version = os.getenv("AZURE_OPENAI_API_VERSION")
58 if not all([api_key, api_endpoint, deployment_name, api_version]):
59 print("Error: AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, or AZURE_OPENAI_DEPLOYMENT_NAME not found in .env file.")
60 return None
61 return openai.AzureOpenAI(
62 api_key=api_key,
63 api_version=api_version,
64 azure_endpoint=api_endpoint,
65 azure_deployment=deployment_name,
66 )
67 if os.getenv("use_anthropic") == "True":
68 self.logger.info("Initializing Anthropic conversation.")
69 api_key = os.getenv("ANTHROPIC_API_KEY")
70 if not api_key:
71 print("Error: ANTHROPIC_API_KEY not found in .env file.")
72 return None
73 return anthropic.Anthropic(api_key=api_key)
74 if os.getenv("use_ollama") == "True":
75 self.logger.info("Initializing Ollama conversation.")
76 return OllamaClient(self.logger,os.getenv("ollama_model_name"))
77
78 raise ValueError("No LLM backend selected.")
79
80 def clean_reply(self, reply):
81 # Remove the code decorators and backticks that ChatGPT returns
82 if reply.startswith('```') and reply.endswith('```'):
83 # Remove the first line as that contains the ``` decorator with whatever language or syntax it is outputting
84 lines = reply.split('\n')
85 cleaned_lines = lines[1:-1]
86 return '\n'.join(cleaned_lines)
87

Callers 2

__init__Method · 0.90
reload_llmMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected