-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSimple_call.py
More file actions
57 lines (44 loc) · 1.74 KB
/
Simple_call.py
File metadata and controls
57 lines (44 loc) · 1.74 KB
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
import os
import sys
from dotenv import load_dotenv
from openai import OpenAI
# Load environment variables from .env file
load_dotenv()
# Active: OpenAI GPT5-nano API call
api_key = os.getenv("OPENAI_API_KEY")
client= OpenAI(
api_key=api_key,
#base_url="http://localhost:1234/v1"
)
model_name="gpt-5-mini"
print("Calling OpenAI GPT5-nano...See how long it takes...")
# function that shows different OpenAI Types of responses.
def showStreamedResponse(streamed_response):
choices = getattr(streamed_response, "choices", None)
if choices is not None:
print(choices[0].message.content)
else:
for chunk in streamed_response:
streamedcontent:str = chunk.choices[0].delta.content
if streamedcontent:
print(streamedcontent, end="")
sys.stdout.flush()
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"},
{"role": "assistant", "content": "Hi there! How can I help you today?"},
{"role": "user", "content": "Can you explain recursion in simple terms?"},
{"role": "assistant", "content": "Sure! Recursion is when a function calls itself to solve smaller parts of a problem."},
{"role": "user", "content": "Tell me a 1000 word story about a programmers and his love of python."}
]
# Calling OpenAI with the OpenAI Python SDK
response = client.chat.completions.create(
model=model_name,
messages=messages,
stream=False
)
print(f"The response from {model_name} is:")
showStreamedResponse(response)
# Want to do it with Google? Let's talk about it!
# google_endpoint ="https://generativelanguage.googleapis.com/v1beta/openai/"
# And on Sunday let's come back here with Microsoft Foundry.