Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async def main():
)

while True:
prompt = input("You: ")
prompt = await asyncio.to_thread(input, "You: ")
if prompt.lower() == "exit":
break
chat.append(user(prompt))
Expand Down
13 changes: 9 additions & 4 deletions examples/aio/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
N = flags.DEFINE_integer("n", 1, "Number of answers to generate.")


async def ainput(prompt: str = "") -> str:
"""Read user input asynchronously without blocking the event loop."""
return await asyncio.to_thread(input, prompt)


async def basic_chat(chat: xai_sdk.aio.chat.Chat):
"""Multi-turn chat between a user and an assistant."""
total_cost_usd = 0.0
while True:
prompt = input("You: ")
prompt = await ainput("You: ")
if prompt.lower() == "exit":
break

Expand All @@ -36,7 +41,7 @@ async def chat_with_streaming(chat: xai_sdk.aio.chat.Chat):
"""Multi-turn chat between a user and an assistant with streaming."""
total_cost_usd = 0.0
while True:
prompt = input("You: ")
prompt = await ainput("You: ")
if prompt.lower() == "exit":
break

Expand Down Expand Up @@ -64,7 +69,7 @@ async def chat_with_streaming(chat: xai_sdk.aio.chat.Chat):
async def batch_chat(chat: xai_sdk.aio.chat.Chat):
"""Multi-turn chat between a user and an assistant with batch sampling."""
while True:
prompt = input("You: ")
prompt = await ainput("You: ")
if prompt.lower() == "exit":
break

Expand All @@ -83,7 +88,7 @@ async def batch_chat(chat: xai_sdk.aio.chat.Chat):
async def batch_chat_with_streaming(chat: xai_sdk.aio.chat.Chat):
"""Multi-turn chat between a user and an assistant with batch sampling and streaming."""
while True:
prompt = input("You: ")
prompt = await ainput("You: ")
if prompt.lower() == "exit":
break

Expand Down