mirror of
https://github.com/Routstr/routstr-core.git
synced 2026-10-05 12:28:22 +00:00
more examples for devs and testing
This commit is contained in:
-37
@@ -1,37 +0,0 @@
|
|||||||
import os
|
|
||||||
|
|
||||||
import openai
|
|
||||||
|
|
||||||
client = openai.OpenAI(
|
|
||||||
api_key=os.environ["CASHU_TOKEN"],
|
|
||||||
base_url=os.environ.get("ROUTSTR_API_URL", "https://api.routstr.com/v1"),
|
|
||||||
# base_url="http://roustrjfsdgfiueghsklchg.onion/v1",
|
|
||||||
# client=httpx.AsyncClient(
|
|
||||||
# proxies={"http": "socks5://localhost:9050"},
|
|
||||||
# ), # to use onion proxy (tor)
|
|
||||||
)
|
|
||||||
history: list = []
|
|
||||||
|
|
||||||
|
|
||||||
def chat() -> None:
|
|
||||||
while True:
|
|
||||||
user_msg = {"role": "user", "content": input("\nYou: ")}
|
|
||||||
history.append(user_msg)
|
|
||||||
ai_msg = {"role": "assistant", "content": ""}
|
|
||||||
|
|
||||||
for chunk in client.chat.completions.create(
|
|
||||||
model=os.environ.get("MODEL", "openai/gpt-4o-mini"),
|
|
||||||
messages=history,
|
|
||||||
stream=True,
|
|
||||||
):
|
|
||||||
if len(chunk.choices) > 0:
|
|
||||||
content = chunk.choices[0].delta.content
|
|
||||||
if content is not None:
|
|
||||||
ai_msg["content"] += content
|
|
||||||
print(content, end="", flush=True)
|
|
||||||
print()
|
|
||||||
history.append(ai_msg)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
chat()
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
# Use your Cashu token or API key as the Bearer token,
|
||||||
|
# cashu token is hashed on the server and acts as an Temporary API key
|
||||||
|
headers = {"Authorization": f"Bearer {os.environ.get('TOKEN')}"}
|
||||||
|
base_url = os.environ.get("API_URL", "https://api.routstr.com/v1")
|
||||||
|
|
||||||
|
resp = httpx.get(f"{base_url}/balance/info", headers=headers)
|
||||||
|
print(resp.json())
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import os
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
# Send a Cashu token to the /create endpoint to get a persistent API key
|
||||||
|
token = os.environ.get("TOKEN")
|
||||||
|
if not token:
|
||||||
|
print("Please set TOKEN environment variable with a Cashu token")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
base_url = os.environ.get("API_URL", "https://api.routstr.com/v1")
|
||||||
|
|
||||||
|
resp = httpx.get(f"{base_url}/balance/create", params={"initial_balance_token": token})
|
||||||
|
|
||||||
|
print(resp.json())
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import os
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
# Use your Cashu token or API key as the Bearer token
|
||||||
|
headers = {"Authorization": f"Bearer {os.environ.get('TOKEN')}"}
|
||||||
|
base_url = os.environ.get("API_URL", "https://api.routstr.com/v1")
|
||||||
|
|
||||||
|
resp = httpx.post(f"{base_url}/balance/refund", headers=headers)
|
||||||
|
|
||||||
|
print("Refund successful!")
|
||||||
|
print(resp.json())
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import os
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
# Use your Cashu token or API key as the Bearer token
|
||||||
|
headers = {"Authorization": f"Bearer {os.environ.get('TOKEN')}"}
|
||||||
|
base_url = os.environ.get("API_URL", "https://api.routstr.com/v1")
|
||||||
|
|
||||||
|
# The Cashu token to top up with
|
||||||
|
cashu_token = input("Enter Cashu token to top up: ")
|
||||||
|
|
||||||
|
resp = httpx.post(
|
||||||
|
f"{base_url}/balance/topup", headers=headers, json={"cashu_token": cashu_token}
|
||||||
|
)
|
||||||
|
|
||||||
|
print(resp.json())
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=os.environ.get("TOKEN"),
|
||||||
|
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
|
||||||
|
)
|
||||||
|
|
||||||
|
response = client.chat.completions.create(
|
||||||
|
model=os.environ.get("MODEL", "gpt-5-nano"),
|
||||||
|
messages=[{"role": "user", "content": "Hello!"}],
|
||||||
|
)
|
||||||
|
|
||||||
|
print(response.choices[0].message.content)
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=os.environ.get("TOKEN", ""),
|
||||||
|
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
|
||||||
|
)
|
||||||
|
|
||||||
|
for model in client.models.list():
|
||||||
|
print(model.id)
|
||||||
|
|
||||||
|
# OR
|
||||||
|
|
||||||
|
models = httpx.get(
|
||||||
|
f"{client.base_url}/v1/models",
|
||||||
|
headers={"Authorization": f"Bearer {client.api_key}"},
|
||||||
|
).json()
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import os
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=os.environ.get("TOKEN"),
|
||||||
|
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
|
||||||
|
)
|
||||||
|
|
||||||
|
conversation = []
|
||||||
|
|
||||||
|
# First turn
|
||||||
|
response1 = client.responses.create( # type: ignore
|
||||||
|
model="o4-mini",
|
||||||
|
input="Hi, my name is Alice.",
|
||||||
|
conversation=conversation,
|
||||||
|
)
|
||||||
|
print("Response 1:", response1.output)
|
||||||
|
|
||||||
|
# Note: The 'conversation' parameter might need to be constructed differently
|
||||||
|
# depending on exact SDK/API spec. Typically, you pass back the previous turn's data.
|
||||||
|
# Assuming the SDK manages or returns a conversation object/ID:
|
||||||
|
# conversation.append(response1)
|
||||||
|
|
||||||
|
# Second turn - demonstrating intent, actual implementation depends on strict API spec
|
||||||
|
# response2 = client.responses.create(
|
||||||
|
# model="openai/gpt-4o-mini",
|
||||||
|
# input="What is my name?",
|
||||||
|
# conversation=conversation,
|
||||||
|
# )
|
||||||
|
# print("Response 2:", response2.output)
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import os
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
|
# The OpenAI SDK handles the 'responses' endpoint if it's updated to the latest version
|
||||||
|
# and the base_url points to a compatible proxy like Routstr.
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=os.environ.get("TOKEN"),
|
||||||
|
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
|
||||||
|
)
|
||||||
|
|
||||||
|
response = client.responses.create(
|
||||||
|
model="gpt-5-mini",
|
||||||
|
input="Tell me a three sentence bedtime story about a unicorn.",
|
||||||
|
)
|
||||||
|
|
||||||
|
print(response.output)
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import os
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=os.environ.get("TOKEN"),
|
||||||
|
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
|
||||||
|
)
|
||||||
|
|
||||||
|
stream = client.responses.create(
|
||||||
|
model="gpt-5-nano",
|
||||||
|
input="Write a short poem about rust.",
|
||||||
|
stream=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
for event in stream:
|
||||||
|
# Note: Depending on the SDK version and response structure,
|
||||||
|
# you might access event.output_delta or similar fields
|
||||||
|
print(event, end="", flush=True)
|
||||||
|
|
||||||
|
print()
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import os
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=os.environ.get("TOKEN"),
|
||||||
|
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
|
||||||
|
)
|
||||||
|
|
||||||
|
stream = client.responses.create(
|
||||||
|
model="claude-4.5-sonnet",
|
||||||
|
input="Write a short poem about rust.",
|
||||||
|
stream=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
for event in stream:
|
||||||
|
# Note: Depending on the SDK version and response structure,
|
||||||
|
# you might access event.output_delta or similar fields
|
||||||
|
print(event, end="", flush=True)
|
||||||
|
print()
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import os
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=os.environ.get("TOKEN"),
|
||||||
|
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
|
||||||
|
)
|
||||||
|
|
||||||
|
response = client.responses.create(
|
||||||
|
model="gpt-5-mini",
|
||||||
|
input="What is the latest news about AI?",
|
||||||
|
tools=[{"type": "web_search"}], # type: ignore
|
||||||
|
)
|
||||||
|
|
||||||
|
print(response.output)
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=os.environ.get("TOKEN"),
|
||||||
|
base_url=os.environ.get("API_URL", "https://api.routstr.com/v1"),
|
||||||
|
)
|
||||||
|
|
||||||
|
messages = []
|
||||||
|
while True:
|
||||||
|
messages.append({"role": "user", "content": input("\nYou: ")})
|
||||||
|
|
||||||
|
stream = client.chat.completions.create(
|
||||||
|
model=os.environ.get("MODEL", "gpt-5.1-mini"),
|
||||||
|
messages=messages, # type: ignore
|
||||||
|
stream=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
print("AI: ", end="")
|
||||||
|
response_content = ""
|
||||||
|
for chunk in stream:
|
||||||
|
if content := chunk.choices[0].delta.content: # type: ignore
|
||||||
|
print(content, end="", flush=True)
|
||||||
|
response_content += content
|
||||||
|
print()
|
||||||
|
|
||||||
|
messages.append({"role": "assistant", "content": response_content})
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import os
|
||||||
|
import httpx
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
|
# Requires `pip install "httpx[socks]"` and a running Tor proxy on port 9050
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=os.environ.get("TOKEN"),
|
||||||
|
base_url=os.environ.get("ONION_URL", "http://roustrjfsdgfiueghsklchg.onion/v1"),
|
||||||
|
http_client=httpx.Client(proxies="socks5://localhost:9050"),
|
||||||
|
)
|
||||||
|
|
||||||
|
print(
|
||||||
|
client.chat.completions.create(
|
||||||
|
model="openai/gpt-4o-mini",
|
||||||
|
messages=[{"role": "user", "content": "Hello from Tor!"}],
|
||||||
|
)
|
||||||
|
.choices[0]
|
||||||
|
.message.content
|
||||||
|
)
|
||||||
@@ -73,6 +73,7 @@ packages = ["routstr"]
|
|||||||
[tool.ruff.lint]
|
[tool.ruff.lint]
|
||||||
select = ["E", "F", "I"]
|
select = ["E", "F", "I"]
|
||||||
ignore = ["E501"]
|
ignore = ["E501"]
|
||||||
|
exclude = ["examples"]
|
||||||
|
|
||||||
[tool.mypy]
|
[tool.mypy]
|
||||||
python_version = "3.11"
|
python_version = "3.11"
|
||||||
|
|||||||
Reference in New Issue
Block a user