Skip to main content

Async Examples

Async Amazon Native​

This demonstrates how to use async-based calls for Amazon AI models.

Converse​

from gen_ai_hub.proxy.native.amazon import AsyncSession
async def async_amazon_bedrock_converse(model_name):
session = AsyncSession()
async with await session.async_client(model_name=model_name) as bedrock:
conversation = [{"role": "user", "content": [{"text": "Describe the purpose of a 'hello world' program in one line."}]}]
response = await bedrock.converse(
messages=conversation,
inferenceConfig={"maxTokens": 512, "temperature": 0.0, "topP": 0.9},
)
print("Response:", response["output"]["message"]["content"][0]["text"])

await async_amazon_bedrock_converse("amazon--nova-lite")

Async Function to Stream Amazon Model Response​

from gen_ai_hub.proxy.native.amazon import AsyncSession
async def async_bedrock_invoke_with_stream():
session = AsyncSession()
bedrock = await session.async_client(model_name="amazon--nova-lite")
conversation = [
{
"role": "user",
"content": [
{"text": "You are a story teller. Tell me a short story about boats."}
],
}
]
response = await bedrock.converse_stream(
messages=conversation,
inferenceConfig={"maxTokens": 512, "temperature": 0.0, "topP": 0.9},
)
async for event in response["stream"]:
delta = event.get("contentBlockDelta", {}).get("delta", {})
if "text" in delta:
print("Chunk Output:", delta["text"])

await async_bedrock_invoke_with_stream()

Embeddings​

import json
from gen_ai_hub.proxy.native.amazon import AsyncSession
async def async_amazon_titan_embedding(model_name):
session = AsyncSession()
async with await session.async_client(model_name=model_name) as bedrock:
body = json.dumps({"inputText": "Please recommend books with a theme similar to the movie 'Inception'."})
response = await bedrock.invoke_model(body=body)
response_body = json.loads(await response.get("body").read())
print("Response Metadata:", response["ResponseMetadata"])
print("Embedding:", response_body["embedding"])

await async_amazon_titan_embedding("amazon--titan-embed-text")

Async Google GenAI Native​

Generate Content​

from gen_ai_hub.proxy.native.google_genai import Client
from gen_ai_hub.proxy import get_proxy_client

proxy_client = get_proxy_client('gen-ai-hub')
async with Client(proxy_client=proxy_client).aio as aclient:
response = await aclient.models.generate_content(
model="gemini-3.5-flash",
contents="Explain the relativity theory in simple terms."
)
print(response)

Chat​

from gen_ai_hub.proxy.native.google_genai import Client
from gen_ai_hub.proxy import get_proxy_client

proxy_client = get_proxy_client('gen-ai-hub')
async with Client(proxy_client=proxy_client).aio as aclient:
chat_session = aclient.chats.create(model="gemini-3.5-flash")
response1 = await chat_session.send_message("Hello.")
print("Response 1:", response1.text)
response2 = await chat_session.send_message("What is your opinion about the latest Gemini model?")
print("Response 2:", response2.text)

Streaming​

from gen_ai_hub.proxy.native.google_genai import Client
from gen_ai_hub.proxy import get_proxy_client
from google.genai.types import GenerateContentConfig, Content, Part

proxy_client = get_proxy_client('gen-ai-hub')
async with Client(proxy_client=proxy_client).aio as aclient:
async_response_stream = await aclient.models.generate_content_stream(
model="gemini-3.5-flash",
contents=[Content(role="user", parts=[Part(text="Write a paragraph about a magic kingdom.")])],
config=GenerateContentConfig(temperature=0),
)
async for chunk in async_response_stream:
print("Chunk:", chunk.text)

LangChain Async Examples​

Async Chat (Amazon Bedrock)​

This demonstrates how to use the chat_model.ainvoke method for the Claude model.

from langchain_core.messages import HumanMessage, AIMessage
from gen_ai_hub.proxy.langchain import ChatBedrock

async def async_amazon_chat_model():
chat_model = ChatBedrock(model_name="anthropic--claude-4.5-haiku", model_kwargs={"temperature": 0.0})
response = await chat_model.ainvoke([HumanMessage(content="Write me a song about sparkling water.")])
if isinstance(response, AIMessage):
print("Response:", response.content)

await async_amazon_chat_model()

Async Streaming (Amazon Bedrock)​

This demonstrates how to use the chat_model.astream method for the Claude model.

from langchain_core.messages import AIMessageChunk, HumanMessage
from gen_ai_hub.proxy.langchain import ChatBedrock

async def async_chat_streaming():
chat_model = ChatBedrock(
model_name="anthropic--claude-4.5-haiku",
model_kwargs={"temperature": 0.0},
streaming=True
)
chunks = []
async for chunk in chat_model.astream([HumanMessage(content="Write me a song about sparkling water in 20 words.")]):
chunks.append(chunk)
print(chunk.content)

assert all(isinstance(chunk, AIMessageChunk) for chunk in chunks)

await async_chat_streaming()

Async Chat (Amazon Bedrock Converse)​

This demonstrates how to use the ChatBedrockConverse model's ainvoke method.

from langchain_core.messages import HumanMessage, AIMessage
from gen_ai_hub.proxy.langchain import ChatBedrockConverse

async def chat_converse_model_example(model_name):
chat_model = ChatBedrockConverse(model_name=model_name, model_kwargs={"temperature": 0.0})
response = await chat_model.ainvoke([HumanMessage(content="Write me a song about sparkling water.")])
if isinstance(response, AIMessage):
print("Response:", response.content)

await chat_converse_model_example("anthropic--claude-4.5-haiku")

Async Gemini​

This demonstrates how to use the ainvoke method of the Gemini model asynchronously.

from gen_ai_hub.proxy.langchain import init_llm

async def gemini_ainvoke_example():
llm = init_llm(model_name="gemini-3.5-flash", max_tokens=300)
response = await llm.ainvoke("Write a ballad about LangChain")
print(response)

await gemini_ainvoke_example()
from gen_ai_hub.proxy.langchain import ChatGoogleGenerativeAI

async def gemini_chat_ainvoke_example():
chat_model = ChatGoogleGenerativeAI(
proxy_model_name="gemini-3.5-flash",
max_tokens=300,
)
response = await chat_model.ainvoke("Write a ballad about LangChain")
print(response)

await gemini_chat_ainvoke_example()

Async Gemini Streaming​

This demonstrates how to use the astream method of the Gemini chat model asynchronously.

from gen_ai_hub.proxy.langchain import ChatGoogleGenerativeAI

async def gemini_astream_example():
chat_model = ChatGoogleGenerativeAI(proxy_model_name="gemini-3.5-flash", temperature=0)
async for chunk in chat_model.astream("Write a story about a magic backpack."):
print("Chunk:", chunk.content)

await gemini_astream_example()