Skip to content

Commit eed5bc5

Browse files
authored
Merge pull request #278 from Routstr/openai-responses-api
OpenAI responses api
2 parents 634a473 + f4b014c commit eed5bc5

17 files changed

Lines changed: 2015 additions & 660 deletions

example.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

examples/balance/check_balance.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
3+
import httpx
4+
5+
# Use your Cashu token or API key as the Bearer token,
6+
# cashu token is hashed on the server and acts as an Temporary API key
7+
headers = {"Authorization": f"Bearer {os.environ.get('TOKEN')}"}
8+
base_url = os.environ.get("API_URL", "https://api.routstr.com/v1")
9+
10+
resp = httpx.get(f"{base_url}/balance/info", headers=headers)
11+
print(resp.json())

examples/balance/create_balance.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
import httpx
4+
5+
# Send a Cashu token to the /create endpoint to get a persistent API key
6+
token = os.environ.get("TOKEN")
7+
if not token:
8+
print("Please set TOKEN environment variable with a Cashu token")
9+
exit(1)
10+
11+
base_url = os.environ.get("API_URL", "https://api.routstr.com/v1")
12+
13+
resp = httpx.get(f"{base_url}/balance/create", params={"initial_balance_token": token})
14+
15+
print(resp.json())

examples/balance/refund_balance.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
3+
import httpx
4+
5+
# Use your Cashu token or API key as the Bearer token
6+
headers = {"Authorization": f"Bearer {os.environ.get('TOKEN')}"}
7+
base_url = os.environ.get("API_URL", "https://api.routstr.com/v1")
8+
9+
resp = httpx.post(f"{base_url}/balance/refund", headers=headers)
10+
11+
print("Refund successful!")
12+
print(resp.json())

examples/balance/topup_balance.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
3+
import httpx
4+
5+
# Use your Cashu token or API key as the Bearer token
6+
headers = {"Authorization": f"Bearer {os.environ.get('TOKEN')}"}
7+
base_url = os.environ.get("API_URL", "https://api.routstr.com/v1")
8+
9+
# The Cashu token to top up with
10+
cashu_token = input("Enter Cashu token to top up: ")
11+
12+
resp = httpx.post(
13+
f"{base_url}/balance/topup", headers=headers, json={"cashu_token": cashu_token}
14+
)
15+
16+
print(resp.json())

examples/chat_completions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
from openai import OpenAI
4+
5+
client = OpenAI(
6+
api_key=os.environ.get("TOKEN"),
7+
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
8+
)
9+
10+
response = client.chat.completions.create(
11+
model=os.environ.get("MODEL", "gpt-5-nano"),
12+
messages=[{"role": "user", "content": "Hello!"}],
13+
)
14+
15+
print(response.choices[0].message.content)

examples/list_models.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
3+
import httpx
4+
from openai import OpenAI
5+
6+
client = OpenAI(
7+
api_key=os.environ.get("TOKEN", ""),
8+
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
9+
)
10+
11+
for model in client.models.list():
12+
print(model.id)
13+
14+
# OR
15+
16+
models = httpx.get(
17+
f"{client.base_url}/v1/models",
18+
headers={"Authorization": f"Bearer {client.api_key}"},
19+
).json()

examples/responses/conversation.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
3+
from openai import OpenAI
4+
5+
client = OpenAI(
6+
api_key=os.environ.get("TOKEN"),
7+
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
8+
)
9+
10+
conversation = [] # type: ignore
11+
12+
# First turn
13+
response1 = client.responses.create( # type: ignore
14+
model="o4-mini",
15+
input="Hi, my name is Alice.",
16+
conversation=conversation,
17+
)
18+
print("Response 1:", response1.output)
19+
20+
# Note: The 'conversation' parameter might need to be constructed differently
21+
# depending on exact SDK/API spec. Typically, you pass back the previous turn's data.
22+
# Assuming the SDK manages or returns a conversation object/ID:
23+
# conversation.append(response1)
24+
25+
# Second turn - demonstrating intent, actual implementation depends on strict API spec
26+
# response2 = client.responses.create(
27+
# model="openai/gpt-4o-mini",
28+
# input="What is my name?",
29+
# conversation=conversation,
30+
# )
31+
# print("Response 2:", response2.output)

examples/responses/create.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
3+
from openai import OpenAI
4+
5+
# The OpenAI SDK handles the 'responses' endpoint if it's updated to the latest version
6+
# and the base_url points to a compatible proxy like Routstr.
7+
client = OpenAI(
8+
api_key=os.environ.get("TOKEN"),
9+
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
10+
)
11+
12+
response = client.responses.create(
13+
model="gpt-5-mini",
14+
input="Tell me a three sentence bedtime story about a unicorn.",
15+
)
16+
17+
print(response.output)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
3+
from openai import OpenAI
4+
5+
client = OpenAI(
6+
api_key=os.environ.get("TOKEN"),
7+
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
8+
)
9+
10+
stream = client.responses.create(
11+
model="claude-4.5-sonnet",
12+
input="Write a short poem about rust.",
13+
stream=True,
14+
)
15+
16+
for event in stream:
17+
# Note: Depending on the SDK version and response structure,
18+
# you might access event.output_delta or similar fields
19+
print(event, end="", flush=True)
20+
print()

0 commit comments

Comments
 (0)