Merge pull request #1899 from pipecat-ai/mb/genai-to-standard-messages
fix: Update GoogleLLMContext to_standard_messages to be compatible with google-genai
This commit is contained in:
@@ -52,8 +52,8 @@ import json
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import google.generativeai as genai
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
from google import genai
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
from pipecat.audio.vad.silero import SileroVADAnalyzer
|
||||||
@@ -71,6 +71,9 @@ from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection
|
|||||||
|
|
||||||
load_dotenv(override=True)
|
load_dotenv(override=True)
|
||||||
|
|
||||||
|
# Initialize the client globally
|
||||||
|
client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
|
||||||
|
|
||||||
|
|
||||||
def get_rag_content():
|
def get_rag_content():
|
||||||
"""Get the RAG content from the file."""
|
"""Get the RAG content from the file."""
|
||||||
@@ -105,20 +108,12 @@ Each request will include:
|
|||||||
Here is the knowledge base you have access to:
|
Here is the knowledge base you have access to:
|
||||||
{RAG_CONTENT}
|
{RAG_CONTENT}
|
||||||
"""
|
"""
|
||||||
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
|
||||||
|
|
||||||
|
|
||||||
async def query_knowledge_base(params: FunctionCallParams):
|
async def query_knowledge_base(params: FunctionCallParams):
|
||||||
"""Query the knowledge base for the answer to the question."""
|
"""Query the knowledge base for the answer to the question."""
|
||||||
logger.info(f"Querying knowledge base for question: {params.arguments['question']}")
|
logger.info(f"Querying knowledge base for question: {params.arguments['question']}")
|
||||||
client = genai.GenerativeModel(
|
|
||||||
model_name=RAG_MODEL,
|
|
||||||
system_instruction=RAG_PROMPT,
|
|
||||||
generation_config=genai.types.GenerationConfig(
|
|
||||||
temperature=0.1,
|
|
||||||
max_output_tokens=64,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
# for our case, the first two messages are the instructions and the user message
|
# for our case, the first two messages are the instructions and the user message
|
||||||
# so we remove them.
|
# so we remove them.
|
||||||
conversation_turns = params.context.messages[2:]
|
conversation_turns = params.context.messages[2:]
|
||||||
@@ -143,8 +138,15 @@ async def query_knowledge_base(params: FunctionCallParams):
|
|||||||
logger.info(f"Conversation turns: {messages_json}")
|
logger.info(f"Conversation turns: {messages_json}")
|
||||||
|
|
||||||
start = time.perf_counter()
|
start = time.perf_counter()
|
||||||
response = client.generate_content(
|
full_prompt = f"System: {RAG_PROMPT}\n\nConversation History: {messages_json}"
|
||||||
contents=[messages_json],
|
|
||||||
|
response = await client.aio.models.generate_content(
|
||||||
|
model=RAG_MODEL,
|
||||||
|
contents=[full_prompt],
|
||||||
|
config={
|
||||||
|
"temperature": 0.1,
|
||||||
|
"max_output_tokens": 64,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
end = time.perf_counter()
|
end = time.perf_counter()
|
||||||
logger.info(f"Time taken: {end - start:.2f} seconds")
|
logger.info(f"Time taken: {end - start:.2f} seconds")
|
||||||
|
|||||||
@@ -367,7 +367,7 @@ class GoogleLLMContext(OpenAILLMContext):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
elif part.function_call:
|
elif part.function_call:
|
||||||
args = type(part.function_call).to_dict(part.function_call).get("args", {})
|
args = part.function_call.args if hasattr(part.function_call, "args") else {}
|
||||||
msg["tool_calls"] = [
|
msg["tool_calls"] = [
|
||||||
{
|
{
|
||||||
"id": part.function_call.name,
|
"id": part.function_call.name,
|
||||||
@@ -382,7 +382,9 @@ class GoogleLLMContext(OpenAILLMContext):
|
|||||||
elif part.function_response:
|
elif part.function_response:
|
||||||
msg["role"] = "tool"
|
msg["role"] = "tool"
|
||||||
resp = (
|
resp = (
|
||||||
type(part.function_response).to_dict(part.function_response).get("response", {})
|
part.function_response.response
|
||||||
|
if hasattr(part.function_response, "response")
|
||||||
|
else {}
|
||||||
)
|
)
|
||||||
msg["tool_call_id"] = part.function_response.name
|
msg["tool_call_id"] = part.function_response.name
|
||||||
msg["content"] = json.dumps(resp)
|
msg["content"] = json.dumps(resp)
|
||||||
|
|||||||
Reference in New Issue
Block a user