1import asyncio
2
3
4
5class MockAsyncClient:
6 """Mock client for demonstration."""
7
8 async def __aenter__(self):
9 return self
10
11 async def __aexit__(self, *args):
12 pass
13
14 async def post(self, url: str, json: dict, timeout: float = 30.0):
15 await asyncio.sleep(0.5)
16
17 class Response:
18 status_code = 200
19 def json(self):
20 return {
21 "choices": [{
22 "message": {
23 "content": f"Response to: {json.get('messages', [{}])[-1].get('content', '')}"
24 }
25 }]
26 }
27 return Response()
28
29async def chat_completion(messages: list[dict]) -> str:
30 """Make a chat completion request."""
31 async with MockAsyncClient() as client:
32 response = await client.post(
33 "https://api.openai.com/v1/chat/completions",
34 json={
35 "model": "gpt-4",
36 "messages": messages
37 },
38 timeout=30.0
39 )
40
41 if response.status_code == 200:
42 data = response.json()
43 return data["choices"][0]["message"]["content"]
44 else:
45 raise Exception(f"API error: {response.status_code}")
46
47async def main():
48 messages = [
49 {"role": "system", "content": "You are helpful."},
50 {"role": "user", "content": "Hello!"}
51 ]
52
53 response = await chat_completion(messages)
54 print(f"AI: {response}")
55
56asyncio.run(main())