Skip to content

conversation state example #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions examples/open_responses/conversation_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

from openai import OpenAI
import os

# Set custom parameters directly
BASE_URL = os.getenv("OPEN_RESPONSES_URL") or "http://localhost:8080/v1" #Either set OPEN_RESPONSES_URL in environment variable or put it directly here.
API_KEY = os.getenv("GROK_API_KEY") or "" #Either set GROK_API_KEY in environment variable or put it directly here.
MODEL_NAME = "qwen-2.5-32b"

# Define custom headers explicitly
custom_headers = {
"Authorization": f"Bearer {API_KEY}"
}

# Create a custom OpenAI client with the custom URL, API key, and explicit headers via default_headers.
client = OpenAI(
base_url=BASE_URL,
api_key=API_KEY,
default_headers=custom_headers
)

history = [
{
"role": "user",
"content": "tell me a joke"
}
]

response = client.responses.create(
model=MODEL_NAME,
input=history,
store=True
)

print(response.output_text)

# Add the response to the conversation

second_response = client.responses.create(
model=MODEL_NAME,
previous_response_id=response.id,
input=[{"role": "user", "content": "explain why this is funny."}],
)
print(second_response.output_text)