-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
103 additions
and
5 deletions.
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
45 changes: 45 additions & 0 deletions
45
.../google_vertex/src/haystack_integrations/components/generators/google_vertex/chat/main.py
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,45 @@ | ||
from vertexai.generative_models import Tool, FunctionDeclaration | ||
|
||
get_current_weather_func = FunctionDeclaration( | ||
name="get_current_weather", | ||
description="Get the current weather in a given location", | ||
parameters={ | ||
"type": "object", | ||
"properties": { | ||
"location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}, | ||
"unit": { | ||
"type": "string", | ||
"enum": [ | ||
"celsius", | ||
"fahrenheit", | ||
], | ||
}, | ||
}, | ||
"required": ["location"], | ||
}, | ||
) | ||
tool = Tool([get_current_weather_func]) | ||
|
||
def get_current_weather(location: str, unit: str = "celsius"): | ||
return {"weather": "sunny", "temperature": 21.8, "unit": unit} | ||
|
||
from haystack_integrations.components.generators.google_vertex import VertexAIGeminiChatGenerator | ||
|
||
|
||
gemini_chat = VertexAIGeminiChatGenerator(project_id="my-project-1487737228087", tools=[tool]) | ||
from haystack.dataclasses import ChatMessage | ||
|
||
|
||
messages = [ChatMessage.from_user("What is the temperature in celsius in Berlin?")] | ||
res = gemini_chat.run(messages=messages) | ||
print ("RESPONSE") | ||
print (res) | ||
print(res["replies"][0].content) | ||
|
||
weather = get_current_weather(**res["replies"][0].content) | ||
|
||
messages += res["replies"] + [ChatMessage.from_function(content=weather, name="get_current_weather")] | ||
|
||
res = gemini_chat.run(messages=messages) | ||
print (res) | ||
print(res["replies"][0].content) |
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