File size: 4,482 Bytes
c126015
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
Example: Using OpenAI SDK with OpenELM API

This example demonstrates how to use the OpenAI SDK (or compatible client)
to call OpenELM models through our OpenAI API compatible wrapper.

Note: The official openai Python package requires the API server to have
proper authentication. For testing, use the included OpenAIClient helper.

Usage:
    python examples/openai_sdk_example.py
"""

import sys
import os

# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app import OpenAIClient


def main():
    """Example usage of the OpenAI-compatible OpenELM API."""
    
    # Create client pointing to our local API
    base_url = os.environ.get("OPENELM_API_URL", "http://localhost:8000")
    client = OpenAIClient(base_url=base_url, api_key="dummy-key")
    
    print("=" * 60)
    print("OpenELM OpenAI API - Usage Example")
    print("=" * 60)
    print(f"API URL: {base_url}")
    print()
    
    # Example 1: Basic chat completion
    print("Example 1: Basic Chat Completion")
    print("-" * 40)
    
    response = client.chat.completions.create(
        model="openelm-450m-instruct",
        messages=[
            {"role": "user", "content": "Say hello in a friendly way!"}
        ],
        max_tokens=100,
        temperature=0.7
    )
    
    print(f"Response ID: {response['id']}")
    print(f"Model: {response['model']}")
    print(f"Content: {response['choices'][0]['message']['content']}")
    print(f"Usage: {response['usage']}")
    print()
    
    # Example 2: Multi-turn conversation
    print("Example 2: Multi-turn Conversation")
    print("-" * 40)
    
    response = client.chat.completions.create(
        model="openelm-450m-instruct",
        messages=[
            {"role": "user", "content": "What is artificial intelligence?"},
            {"role": "assistant", "content": "Artificial intelligence (AI) refers to systems that can perform tasks that typically require human intelligence."},
            {"role": "user", "content": "What are some examples?"}
        ],
        max_tokens=150,
        temperature=0.5
    )
    
    print(f"Content: {response['choices'][0]['message']['content']}")
    print(f"Usage: {response['usage']}")
    print()
    
    # Example 3: Using system message
    print("Example 3: Using System Message")
    print("-" * 40)
    
    response = client.chat.completions.create(
        model="openelm-450m-instruct",
        messages=[
            {"role": "system", "content": "You are a helpful coding assistant."},
            {"role": "user", "content": "What is a Python decorator?"}
        ],
        max_tokens=200,
        temperature=0.8
    )
    
    print(f"Content: {response['choices'][0]['message']['content']}")
    print(f"Usage: {response['usage']}")
    print()
    
    # Example 4: Deterministic generation (temperature=0)
    print("Example 4: Deterministic Generation (temperature=0)")
    print("-" * 40)
    
    response = client.chat.completions.create(
        model="openelm-450m-instruct",
        messages=[
            {"role": "user", "content": "What is 2 + 2?"}
        ],
        max_tokens=50,
        temperature=0.0  # Deterministic output
    )
    
    print(f"Content: {response['choices'][0]['message']['content']}")
    print(f"Usage: {response['usage']}")
    print()
    
    # Example 5: Streaming response
    print("Example 5: Streaming Response")
    print("-" * 40)
    print("Streaming response:")
    
    response = client.chat.completions.create(
        model="openelm-450m-instruct",
        messages=[
            {"role": "user", "content": "Count to 5, one number per line."}
        ],
        max_tokens=100,
        temperature=0.7,
        stream=True
    )
    
    # For streaming, response is a generator
    chunk_count = 0
    for chunk in response:
        if 'choices' in chunk and chunk['choices']:
            delta = chunk['choices'][0].get('delta', {})
            if 'content' in delta:
                content = delta['content']
                if content:
                    print(content, end="", flush=True)
                    chunk_count += 1
        elif 'error' in chunk:
            print(f"Error: {chunk['error']}")
            break
    
    print("\n")
    print(f"Received {chunk_count} chunks")
    print()
    
    print("=" * 60)
    print("All examples completed successfully!")
    print("=" * 60)


if __name__ == "__main__":
    main()