Skip to content

Commit 2edc4c3

Browse files
docs: add examples based on uv run scripts (#154)
1 parent 6808eae commit 2edc4c3

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

examples/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Collection of Python galileo examples
2+
3+
### Preconditions
4+
5+
Install `uv`, we use inline dependency inside scripts.
6+
7+
### How to use/run?
8+
9+
First of all create `.env` file and add required env vars based on `.env.sample`.
10+
11+
Then just run `uv`:
12+
13+
```bash
14+
uv run --env-file=examples/langgraph/.env examples/langgraph/with_openai.py
15+
```
16+
17+
or
18+
19+
#### [basic_langgraph.py]
20+
```bash
21+
uv run --env-file=examples/langgraph/.env examples/langgraph/basic_langgraph.py
22+
```

examples/langgraph/.env.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
GALILEO_API_KEY=
2+
GALILEO_PROJECT=
3+
GALILEO_LOG_STREAM=
4+
OPENAI_API_KEY=

examples/langgraph/basic_langgraph.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# /// script
2+
# requires-python = ">=3.10"
3+
# dependencies = [
4+
# "galileo",
5+
# "langgraph",
6+
# "langsmith",
7+
# "langchain",
8+
# "grandalf", # for printing graph in ascii
9+
# ]
10+
# ///
11+
# from dotenv import load_dotenv; load_dotenv()
12+
13+
from typing import Annotated
14+
15+
from langchain_core.messages import AIMessage
16+
from langgraph.graph import END, START, StateGraph
17+
from langgraph.graph.message import add_messages
18+
from typing_extensions import TypedDict
19+
20+
from galileo.handlers.langchain import GalileoCallback
21+
22+
23+
class State(TypedDict):
24+
# Messages have the type "list". The `add_messages` function
25+
# in the annotation defines how this state key should be updated
26+
# (in this case, it appends messages to the list, rather than overwriting them)
27+
messages: Annotated[list, add_messages]
28+
29+
30+
def node(state: State):
31+
messages = state["messages"]
32+
new_message = AIMessage("Hello!")
33+
34+
return {"messages": messages + [new_message], "extra_field": 10}
35+
36+
37+
def node2(state: State):
38+
return {"messages": state["messages"]}
39+
40+
41+
graph_builder = StateGraph(State)
42+
graph_builder.add_node("node_name", node)
43+
graph_builder.add_edge(START, "node_name")
44+
graph_builder.add_edge("node_name", END)
45+
graph = graph_builder.compile()
46+
47+
graph.get_graph().print_ascii()
48+
graph.invoke({"messages": [{"role": "user", "content": "hi!"}]}, config={"callbacks": [GalileoCallback()]})

examples/langgraph/with_openai.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# /// script
2+
# requires-python = ">=3.10"
3+
# dependencies = [
4+
# "galileo",
5+
# "langgraph",
6+
# "langsmith",
7+
# "langchain[openai]",
8+
# "grandalf", # for printing graph in ascii
9+
# ]
10+
# ///
11+
from typing import Annotated
12+
13+
from langchain_openai import ChatOpenAI
14+
from langgraph.graph import START, StateGraph
15+
from langgraph.graph.message import add_messages
16+
from typing_extensions import TypedDict
17+
18+
from galileo.handlers.langchain import GalileoCallback
19+
20+
21+
class State(TypedDict):
22+
# Messages have the type "list". The `add_messages` function
23+
# in the annotation defines how this state key should be updated
24+
# (in this case, it appends messages to the list, rather than overwriting them)
25+
messages: Annotated[list, add_messages]
26+
27+
28+
llm = ChatOpenAI(model="gpt-4")
29+
30+
31+
def chatbot(state: State):
32+
return {"messages": [llm.invoke(state["messages"])]}
33+
34+
35+
graph_builder = StateGraph(State)
36+
# The first argument is the unique node name
37+
# The second argument is the function or object that will be called whenever
38+
# the node is used.
39+
graph_builder.add_node("chatbot", chatbot)
40+
graph_builder.add_edge(START, "chatbot")
41+
graph = graph_builder.compile()
42+
43+
graph.get_graph().print_ascii()
44+
45+
graph.invoke({"messages": [{"role": "user", "content": "hi!"}]}, {"callbacks": [GalileoCallback()]})

0 commit comments

Comments
 (0)