Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions jev-python/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENROUTER_API_KEY=your-openrouter-api-key
3 changes: 3 additions & 0 deletions jev-python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
.venv/
__pycache__/
15 changes: 15 additions & 0 deletions jev-python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# How to Get Started With Jev in Python

Sample code for the Real Python tutorial [How to Get Started With Jev in Python](https://realpython.com/jev-python/).

## Setup

Create a `.env` file with your OpenRouter API key (see `.env.example`), then run the scripts with uv:

```console
$ uv run plain_python.py
$ uv run --env-file .env jev_noul.py
$ uv run --env-file .env jev_desk.py
```

uv reads `pyproject.toml` and installs `typesafe-sdk` on the first run. If you prefer pip, install the pinned dependency from `requirements.txt` instead.
49 changes: 49 additions & 0 deletions jev-python/jev_desk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
from pprint import pprint

from typesafe_sdk import Choice, Noul, Score, TypeSafeClient

client = TypeSafeClient(
api_key=os.environ["OPENROUTER_API_KEY"],
base_url="https://openrouter.ai/api",
)

QUESTIONS = {
"desk": Choice(
instructions="Where should the info desk send the visitor?",
criteria={
"ticket_counter": "Buying, changing, or refunding tickets.",
"lost_and_found": "Looking for something they lost.",
"immediate_help": "An emergency, an injury, or a missing person.",
},
),
"urgency": Score(
instructions="How urgent is the request of the visitor?",
criteria=[
"Not urgent at all.",
"Should be handled today.",
"Should be handled within the next few minutes.",
"Needs to be handled right now.",
],
),
"needs_assistance": Noul(
instructions=(
"Does the visitor need a staff member to help them get around "
"the station, for example because of a wheelchair, heavy "
"luggage, or small children?"
),
),
}


def main():
visitor_says = input("What does the visitor say? ")
r = client.system_one(
state={"visitor_says": visitor_says},
questions=QUESTIONS,
)
pprint(r.model_dump()["answers"])


if __name__ == "__main__":
main()
43 changes: 43 additions & 0 deletions jev-python/jev_noul.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os

from typesafe_sdk import Noul, TypeSafeClient

client = TypeSafeClient(
api_key=os.environ["OPENROUTER_API_KEY"],
base_url="https://openrouter.ai/api",
)


def respond(lost_something):
if lost_something:
print("You can find the lost and found counter on the right.")
else:
print("What can I help you with?")


def ask():
while True:
answer = input("Did you lose something? ")
r = client.system_one(
state=answer,
questions={
"lost_something": Noul(
instructions="Is the answer from the user affirmative?"
),
},
)
lost_something = r.answers["lost_something"].noul
if lost_something > 0.8:
return True
if lost_something < 0.2:
return False
print("Sorry, I didn't get that.")


def main():
lost_something = ask()
respond(lost_something)


if __name__ == "__main__":
main()
24 changes: 24 additions & 0 deletions jev-python/plain_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def respond(lost_something):
if lost_something:
print("You can find the lost and found counter on the right.")
else:
print("What can I help you with?")


def ask():
while True:
answer = input("Did you lose something? (Y/N) ")
if answer == "Y":
return True
if answer == "N":
return False
print("Please answer with Y or N.")


def main():
lost_something = ask()
respond(lost_something)


if __name__ == "__main__":
main()
7 changes: 7 additions & 0 deletions jev-python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
name = "jev-python"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = [
"typesafe-sdk==0.7.1",
]
1 change: 1 addition & 0 deletions jev-python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typesafe-sdk==0.7.1
Loading