Samfy454 / example.py
Samfy001's picture
Upload 6 files
f6de122 verified
#!/usr/bin/env python3
"""
Example usage of the v0.dev OpenAI Compatible API
"""
import openai
import requests
# Configuration
BASE_URL = "http://localhost:8000/v1"
API_KEY = "dummy" # Not used but required by OpenAI client
# Initialize client
client = openai.OpenAI(
base_url=BASE_URL,
api_key=API_KEY
)
def basic_chat():
"""Basic chat completion example"""
print("=== Basic Chat Example ===")
response = client.chat.completions.create(
model="v0-gpt-5",
messages=[
{"role": "system", "content": "You are an expert React developer"},
{"role": "user", "content": "Create a simple todo list component in React"}
],
max_tokens=1000,
temperature=0.7
)
print("Response:")
print(response.choices[0].message.content)
print()
def streaming_chat():
"""Streaming chat completion example"""
print("=== Streaming Chat Example ===")
stream = client.chat.completions.create(
model="v0-gpt-5",
messages=[
{"role": "user", "content": "Explain React hooks in simple terms"}
],
stream=True
)
print("Response (streaming):")
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
print()
def list_models():
"""List available models"""
print("=== Available Models ===")
models = client.models.list()
for model in models.data:
print(f"- {model.id} (owned by {model.owned_by})")
print()
def main():
"""Run all examples"""
try:
# Test health endpoint
health = requests.get("http://localhost:8000/health")
if health.status_code != 200:
print("Server is not running. Please start the server first:")
print("python main.py")
return
# Run examples
list_models()
basic_chat()
streaming_chat()
except Exception as e:
print(f"Error: {e}")
print("Make sure the server is running: python main.py")
if __name__ == "__main__":
main()