Skip to content

Commit f0e73b6

Browse files
authored
Merge branch 'master' into langchain-rag-app
2 parents 2daed73 + b20e04a commit f0e73b6

51 files changed

Lines changed: 485 additions & 12 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎instance-class-static-methods/pizza.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ def prosciutto(cls):
2222

2323
@staticmethod
2424
def get_size_in_inches(size):
25-
"""Returns an approximate diameter in inches for common sizes."""
26-
size_map = {"small": 8, "medium": 12, "large": 16}
25+
"""Returns the diameter in inches for common pizza sizes."""
26+
size_map = {
27+
"small": 8,
28+
"medium": 12,
29+
"large": 16,
30+
}
2731
return size_map.get(size, "Unknown size")
2832

2933

‎jev-python/.env.example‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OPENROUTER_API_KEY=your-openrouter-api-key

‎jev-python/.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.env
2+
.venv/
3+
__pycache__/

‎jev-python/README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# How to Get Started With Jev in Python
2+
3+
Sample code for the Real Python tutorial [How to Get Started With Jev in Python](https://realpython.com/jev-python/).
4+
5+
## Setup
6+
7+
Create a `.env` file with your OpenRouter API key (see `.env.example`), then run the scripts with uv:
8+
9+
```console
10+
$ uv run plain_python.py
11+
$ uv run --env-file .env jev_noul.py
12+
$ uv run --env-file .env jev_desk.py
13+
```
14+
15+
uv reads `pyproject.toml` and installs `typesafe-sdk` on the first run. If you prefer pip, install the pinned dependency from `requirements.txt` instead.

‎jev-python/jev_desk.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
from pprint import pprint
3+
4+
from typesafe_sdk import Choice, Noul, Score, TypeSafeClient
5+
6+
client = TypeSafeClient(
7+
api_key=os.environ["OPENROUTER_API_KEY"],
8+
base_url="https://openrouter.ai/api",
9+
)
10+
11+
QUESTIONS = {
12+
"desk": Choice(
13+
instructions="Where should the info desk send the visitor?",
14+
criteria={
15+
"ticket_counter": "Buying, changing, or refunding tickets.",
16+
"lost_and_found": "Looking for something they lost.",
17+
"immediate_help": "An emergency, an injury, or a missing person.",
18+
},
19+
),
20+
"urgency": Score(
21+
instructions="How urgent is the request of the visitor?",
22+
criteria=[
23+
"Not urgent at all.",
24+
"Should be handled today.",
25+
"Should be handled within the next few minutes.",
26+
"Needs to be handled right now.",
27+
],
28+
),
29+
"needs_assistance": Noul(
30+
instructions=(
31+
"Does the visitor need a staff member to help them get around "
32+
"the station, for example because of a wheelchair, heavy "
33+
"luggage, or small children?"
34+
),
35+
),
36+
}
37+
38+
39+
def main():
40+
visitor_says = input("What does the visitor say? ")
41+
r = client.system_one(
42+
state={"visitor_says": visitor_says},
43+
questions=QUESTIONS,
44+
)
45+
pprint(r.model_dump()["answers"])
46+
47+
48+
if __name__ == "__main__":
49+
main()

‎jev-python/jev_noul.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
3+
from typesafe_sdk import Noul, TypeSafeClient
4+
5+
client = TypeSafeClient(
6+
api_key=os.environ["OPENROUTER_API_KEY"],
7+
base_url="https://openrouter.ai/api",
8+
)
9+
10+
11+
def respond(lost_something):
12+
if lost_something:
13+
print("You can find the lost and found counter on the right.")
14+
else:
15+
print("What can I help you with?")
16+
17+
18+
def ask():
19+
while True:
20+
answer = input("Did you lose something? ")
21+
r = client.system_one(
22+
state=answer,
23+
questions={
24+
"lost_something": Noul(
25+
instructions="Is the answer from the user affirmative?"
26+
),
27+
},
28+
)
29+
lost_something = r.answers["lost_something"].noul
30+
if lost_something > 0.8:
31+
return True
32+
if lost_something < 0.2:
33+
return False
34+
print("Sorry, I didn't get that.")
35+
36+
37+
def main():
38+
lost_something = ask()
39+
respond(lost_something)
40+
41+
42+
if __name__ == "__main__":
43+
main()

‎jev-python/plain_python.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def respond(lost_something):
2+
if lost_something:
3+
print("You can find the lost and found counter on the right.")
4+
else:
5+
print("What can I help you with?")
6+
7+
8+
def ask():
9+
while True:
10+
answer = input("Did you lose something? (Y/N) ")
11+
if answer == "Y":
12+
return True
13+
if answer == "N":
14+
return False
15+
print("Please answer with Y or N.")
16+
17+
18+
def main():
19+
lost_something = ask()
20+
respond(lost_something)
21+
22+
23+
if __name__ == "__main__":
24+
main()

‎jev-python/pyproject.toml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
name = "jev-python"
3+
version = "0.1.0"
4+
requires-python = ">=3.10"
5+
dependencies = [
6+
"typesafe-sdk==0.7.1",
7+
]

‎jev-python/requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
typesafe-sdk==0.7.1
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
def add_product(item, inventory=[]):
2-
inventory.append(item)
3-
return inventory
1+
def append_to(item, target=[]):
2+
target.append(item)
3+
return target
44

55

6-
print(add_product("apple"))
7-
print(add_product("banana"))
6+
print(append_to(1))
7+
print(append_to(2))
8+
print(append_to(3))

0 commit comments

Comments
 (0)