From 2245f4690eb7aa111508b72c8f615f0d0b7d949e Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 15 Nov 2024 11:02:27 +0000 Subject: [PATCH 1/3] fix: reddit loader validation --- application/parser/remote/reddit_loader.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/application/parser/remote/reddit_loader.py b/application/parser/remote/reddit_loader.py index 0230653a..22f5b185 100644 --- a/application/parser/remote/reddit_loader.py +++ b/application/parser/remote/reddit_loader.py @@ -1,10 +1,19 @@ from application.parser.remote.base import BaseRemote from langchain_community.document_loaders import RedditPostsLoader +import json class RedditPostsLoaderRemote(BaseRemote): def load_data(self, inputs): - data = eval(inputs) + try: + data = json.loads(inputs) + except json.JSONDecodeError as e: + raise ValueError(f"Invalid JSON input: {e}") + + required_fields = ["client_id", "client_secret", "user_agent", "search_queries"] + missing_fields = [field for field in required_fields if field not in data] + if missing_fields: + raise ValueError(f"Missing required fields: {', '.join(missing_fields)}") client_id = data.get("client_id") client_secret = data.get("client_secret") user_agent = data.get("user_agent") From a4c95fd62b8e6e731b40b47a87ebb6f4255bfade Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 15 Nov 2024 12:17:25 +0000 Subject: [PATCH 2/3] feat: add google ai --- application/llm/google_ai.py | 52 ++++++++++++++++++++++++++++++++++ application/llm/llm_creator.py | 4 ++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 application/llm/google_ai.py diff --git a/application/llm/google_ai.py b/application/llm/google_ai.py new file mode 100644 index 00000000..ea460345 --- /dev/null +++ b/application/llm/google_ai.py @@ -0,0 +1,52 @@ +from application.llm.base import BaseLLM +from application.core.settings import settings + + + + +class GoogleLLM(BaseLLM): + + def __init__(self, api_key=None, user_api_key=None, *args, **kwargs): + + super().__init__(*args, **kwargs) + self.api_key = api_key + self.user_api_key = user_api_key + + def _clean_messages_google(self, messages): + return [ + { + "role": "model" if message["role"] == "system" else message["role"], + "parts": [message["content"]], + } + for message in messages[1:] + ] + + def _raw_gen( + self, + baseself, + model, + messages, + stream=False, + **kwargs + ): + import google.generativeai as genai + genai.configure(api_key=self.api_key) + model = genai.GenerativeModel(model, system_instruction=messages[0]["content"]) + response = model.generate_content(self._clean_messages_google(messages)) + return response.text + + def _raw_gen_stream( + self, + baseself, + model, + messages, + stream=True, + **kwargs + ): + import google.generativeai as genai + genai.configure(api_key=self.api_key) + model = genai.GenerativeModel(model, system_instruction=messages[0]["content"]) + response = model.generate_content(self._clean_messages_google(messages), stream=True) + for line in response: + if line.text is not None: + yield line.text \ No newline at end of file diff --git a/application/llm/llm_creator.py b/application/llm/llm_creator.py index 6a19de10..f32089de 100644 --- a/application/llm/llm_creator.py +++ b/application/llm/llm_creator.py @@ -6,6 +6,7 @@ from application.llm.llama_cpp import LlamaCpp from application.llm.anthropic import AnthropicLLM from application.llm.docsgpt_provider import DocsGPTAPILLM from application.llm.premai import PremAILLM +from application.llm.google_ai import GoogleLLM class LLMCreator: @@ -18,7 +19,8 @@ class LLMCreator: "anthropic": AnthropicLLM, "docsgpt": DocsGPTAPILLM, "premai": PremAILLM, - "groq": GroqLLM + "groq": GroqLLM, + "google": GoogleLLM } @classmethod From 144ab61e079cb37fa93baa4095d5ae569b66adbf Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 15 Nov 2024 12:19:43 +0000 Subject: [PATCH 3/3] fix: ruff lint --- application/llm/google_ai.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/application/llm/google_ai.py b/application/llm/google_ai.py index ea460345..df252abf 100644 --- a/application/llm/google_ai.py +++ b/application/llm/google_ai.py @@ -1,8 +1,4 @@ from application.llm.base import BaseLLM -from application.core.settings import settings - - - class GoogleLLM(BaseLLM):