File size: 2,154 Bytes
f6de122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/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()