Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add google ai #1441

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions application/llm/google_ai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from application.llm.base import BaseLLM

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

Check warning on line 9 in application/llm/google_ai.py

View check run for this annotation

Codecov / codecov/patch

application/llm/google_ai.py#L7-L9

Added lines #L7 - L9 were not covered by tests

def _clean_messages_google(self, messages):
return [

Check warning on line 12 in application/llm/google_ai.py

View check run for this annotation

Codecov / codecov/patch

application/llm/google_ai.py#L12

Added line #L12 was not covered by tests
{
"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

Check warning on line 32 in application/llm/google_ai.py

View check run for this annotation

Codecov / codecov/patch

application/llm/google_ai.py#L28-L32

Added lines #L28 - L32 were not covered by tests

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

Check warning on line 48 in application/llm/google_ai.py

View check run for this annotation

Codecov / codecov/patch

application/llm/google_ai.py#L42-L48

Added lines #L42 - L48 were not covered by tests
4 changes: 3 additions & 1 deletion application/llm/llm_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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:
Expand All @@ -18,7 +19,8 @@ class LLMCreator:
"anthropic": AnthropicLLM,
"docsgpt": DocsGPTAPILLM,
"premai": PremAILLM,
"groq": GroqLLM
"groq": GroqLLM,
"google": GoogleLLM
}

@classmethod
Expand Down