Skip to content

Instantly share code, notes, and snippets.

@0xblankspace
Last active March 10, 2024 11:23
Show Gist options
  • Save 0xblankspace/6a49da11b0a289a03c0947f8c7d35c29 to your computer and use it in GitHub Desktop.
Save 0xblankspace/6a49da11b0a289a03c0947f8c7d35c29 to your computer and use it in GitHub Desktop.
Kuzco Inference - Interactive Terminal (mixtral, non-streaming, python)
import asyncio
import aiohttp
import json
import sys
API_KEY = '[INSERT YOUR KUZCO API KEY HERE]'
BASE_URL = 'https://relay.kuzco.xyz/v1'
async def main():
messages = []
print("\n")
while True:
user_input = input("User: ")
if user_input.lower() == 'exit':
break
messages.append({'role': 'user', 'content': user_input + '\n'})
timeout = aiohttp.ClientTimeout(total=5*60)
async with aiohttp.ClientSession(timeout=timeout) as session:
try:
async with session.post(
f'{BASE_URL}/chat/completions',
headers={
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
},
json={
'messages': messages,
'model': 'mistral',
'stream': False
}
) as response:
content = await response.text()
print(f"\nKuzco (Mistral):\n\n{json.loads(content).get('choices', [{}])[0].get('message', {}).get('content', '').lstrip()}\n\n")
except asyncio.TimeoutError:
print("Request timed out")
except Exception as e:
print(f"An error occurred: {e}")
def run():
asyncio.run(main())
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment