Fix: Migrate Gemini Embeddings (#3002)
Co-authored-by: Dev-Khant <devkhant24@gmail.com>
This commit is contained in:
@@ -4,7 +4,7 @@ from typing import Dict, List, Optional
|
||||
try:
|
||||
from google import genai
|
||||
from google.genai import types
|
||||
|
||||
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"The 'google-generativeai' library is required. Please install it using 'pip install google-generativeai'."
|
||||
@@ -49,16 +49,17 @@ class GeminiLLM(LLMBase):
|
||||
for part in candidate.content.parts:
|
||||
fn = getattr(part, "function_call", None)
|
||||
if fn:
|
||||
processed_response["tool_calls"].append({
|
||||
"name": fn.name,
|
||||
"arguments": fn.args,
|
||||
})
|
||||
processed_response["tool_calls"].append(
|
||||
{
|
||||
"name": fn.name,
|
||||
"arguments": fn.args,
|
||||
}
|
||||
)
|
||||
|
||||
return processed_response
|
||||
|
||||
return content
|
||||
|
||||
|
||||
def _reformat_messages(self, messages: List[Dict[str, str]]) -> List[types.Content]:
|
||||
"""
|
||||
Reformat messages for Gemini using google.genai.types.
|
||||
@@ -78,15 +79,11 @@ class GeminiLLM(LLMBase):
|
||||
content = message["content"]
|
||||
|
||||
new_messages.append(
|
||||
types.Content(
|
||||
role="model" if message["role"] == "model" else "user",
|
||||
parts=[types.Part(text=content)]
|
||||
)
|
||||
types.Content(role="model" if message["role"] == "model" else "user", parts=[types.Part(text=content)])
|
||||
)
|
||||
|
||||
return new_messages
|
||||
|
||||
|
||||
def _reformat_tools(self, tools: Optional[List[Dict]]):
|
||||
"""
|
||||
Reformat tools for Gemini.
|
||||
@@ -131,7 +128,6 @@ class GeminiLLM(LLMBase):
|
||||
tools: Optional[List[Dict]] = None,
|
||||
tool_choice: str = "auto",
|
||||
):
|
||||
|
||||
"""
|
||||
Generate a response based on the given messages using Gemini.
|
||||
|
||||
@@ -161,31 +157,22 @@ class GeminiLLM(LLMBase):
|
||||
tool_config = types.ToolConfig(
|
||||
function_calling_config=types.FunctionCallingConfig(
|
||||
mode=tool_choice.upper(), # Assuming 'any' should become 'ANY', etc.
|
||||
allowed_function_names=[
|
||||
tool["function"]["name"] for tool in tools
|
||||
] if tool_choice == "any" else None
|
||||
allowed_function_names=[tool["function"]["name"] for tool in tools]
|
||||
if tool_choice == "any"
|
||||
else None,
|
||||
)
|
||||
)
|
||||
|
||||
print(f"Tool config: {tool_config}")
|
||||
print(f"Params: {params}" )
|
||||
print(f"Messages: {messages}")
|
||||
print(f"Tools: {tools}")
|
||||
print(f"Reformatted messages: {self._reformat_messages(messages)}")
|
||||
print(f"Reformatted tools: {self._reformat_tools(tools)}")
|
||||
|
||||
response = self.client_gemini.models.generate_content(
|
||||
model=self.config.model,
|
||||
contents=self._reformat_messages(messages),
|
||||
config=types.GenerateContentConfig(
|
||||
temperature= self.config.temperature,
|
||||
max_output_tokens= self.config.max_tokens,
|
||||
top_p= self.config.top_p,
|
||||
tools=self._reformat_tools(tools),
|
||||
tool_config=tool_config,
|
||||
|
||||
),
|
||||
)
|
||||
print(f"Response test: {response}")
|
||||
model=self.config.model,
|
||||
contents=self._reformat_messages(messages),
|
||||
config=types.GenerateContentConfig(
|
||||
temperature=self.config.temperature,
|
||||
max_output_tokens=self.config.max_tokens,
|
||||
top_p=self.config.top_p,
|
||||
tools=self._reformat_tools(tools),
|
||||
tool_config=tool_config,
|
||||
),
|
||||
)
|
||||
|
||||
return self._parse_response(response, tools)
|
||||
|
||||
Reference in New Issue
Block a user