-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1441 from arc53/google-llm
- Loading branch information
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
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
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 | ||
|
||
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 |
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