-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgemini.py
More file actions
52 lines (41 loc) · 1.72 KB
/
gemini.py
File metadata and controls
52 lines (41 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import asyncio
import os
from google import genai
from mcp import ClientSession, StdioServerParameters, stdio_client
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
# Create server parameters for stdio connection
server_params = StdioServerParameters(
command="python", # Executable
args=["server.py"],
)
async def run():
async with stdio_client(server_params) as (read, write):
async with ClientSession(
read,
write,
) as session:
await session.initialize()
# Initialize conversation history using simple tuples
config = genai.types.GenerateContentConfig(
temperature=0,
tools=[session],
)
print("Agent is ready. Type 'exit' to quit.")
chat = client.aio.chats.create(
model="gemini-2.5-flash", config=config
)
while (user_input := input("You: ")).lower() != "exit":
# Append user message to history
response = await chat.send_message(user_input)
if len(response.automatic_function_calling_history):
for call in response.automatic_function_calling_history:
if call.parts[0].function_call:
print(f"Function call: {call.parts[0].function_call}")
elif call.parts[0].function_response:
print(
f"Function response: {call.parts[0].function_response.response['result'].content[0].text}"
)
print(f"Assistant: {response.text}")
print("Goodbye.")
if __name__ == "__main__":
asyncio.run(run())