File size: 771 Bytes
495399f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import asyncio
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.messages import HumanMessage

load_dotenv()

async def test_stream():
    print("Testing Gemini Streaming...")
    
    llm = ChatGoogleGenerativeAI(
        model="gemini-2.5-flash",
        temperature=0,
        streaming=True,
        api_key=os.getenv("GOOGLE_API_KEY")
    )
    
    print("Invoking astream...")
    try:
        async for chunk in llm.astream([HumanMessage(content="Halo, hitung 1 sampai 5!")]):
            print(f"Chunk received: {chunk.content}", end="|", flush=True)
        print("\nDone.")
    except Exception as e:
        print(f"\nError: {e}")

if __name__ == "__main__":
    asyncio.run(test_stream())