import cycls
@cycls.app(pip=["google-genai"], copy=[".env"])
async def app(context):
import os
from google import genai
# Convert messages to Gemini format
contents = [
{
'role': 'model' if m['role'] == 'assistant' else 'user',
'parts': [{'text': m['content']}]
}
for m in context.messages if m['role'] != 'system'
]
# Initialize Gemini client
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
# Stream the response
async for chunk in await client.aio.models.generate_content_stream(
model="gemini-2.5-flash",
contents=contents,
config={'system_instruction': "You are a helpful AI assistant."}
):
if chunk.text:
yield chunk.text
app.local()