Skip to content

Instantly share code, notes, and snippets.

@hxy9243
Created August 29, 2024 21:45
Show Gist options
  • Save hxy9243/677107457257de312a569a5f1be88524 to your computer and use it in GitHub Desktop.
Save hxy9243/677107457257de312a569a5f1be88524 to your computer and use it in GitHub Desktop.
OpenAI compatible FastAPI
import os
from openai import OpenAI
URL="https://fast-api.snova.ai/v1/",
API_KEY="xxxx"
def main():
client = OpenAI(
base_url="https://fast-api.snova.ai/v1/",
api_key="aHh5OTI0M19fZ21haWwuY29tOk5EVWpnYnc5NDM=",
)
m = {'role': 'system', 'content': 'You are a helpful assistant who speaks in an elegant and professional fashion.'}
conversation_history = [m]
while True:
try:
user_input = input("You: ")
new_message = {'role': 'user', 'content': user_input}
conversation_history.append(new_message)
response = client.chat.completions.create(
model='llama3-8b',
messages=conversation_history,
stream=True,
extra_headers={'Authorization': 'Basic ' + API_KEY}
)
for chunk in response:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
print()
except KeyboardInterrupt:
print("\nUser Interruption Detected. Goodbye!")
break
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment