Skip to content

Commit 1edabcd

Browse files
authored
[WEAVE] Migrate examples folder from old docs site (#1879)
## Description This migrates working examples from the old Weave docs site to this one. Scott C. and I will work on possibly properly reintegrating them into the docs.
1 parent bf5a28f commit 1edabcd

File tree

90 files changed

+5623
-0
lines changed

Some content is hidden

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

90 files changed

+5623
-0
lines changed

weave/examples/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local_artifacts
2+
MNIST
3+
aclImdb
4+
aclImdb_v1.tar.gz
5+
wandb/

weave/examples/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Weave Examples
2+
3+
## Examples structure
4+
5+
The `examples/` folder contains:
6+
7+
- `get_started.ipynb`: a quick interactive notebook overview of Weave
8+
- `getting_started`: Learn how to build interactive data applications
9+
- `reference`: Syntax and usage for Weave types, ops, and panels
10+
- `monitoring`: Examples and documentation for LLM monitoring
11+
- `experimental`: Everything else, ranging from focused proofs-of-concept to very experimental works-in-progress—we hope these inspire your exploration and welcome PRs for improvement!
12+
- `apps`: Example applications
13+
- `guides (future)`: Guides to use specific features for advanced users
14+
15+
## Running examples
16+
17+
### Locally with Jupyter notebook
18+
19+
Run `jupyter notebook` or `jupyter lab` in the root directory `examples`, and then use the Jupyter browser to find and open.
20+
21+
### Google Colab
22+
23+
Run the same code in a Google Colab, making sure to install Weave from PyPi first with `!pip install weave`.
24+
25+
Colabs to get started:
26+
27+
- [Weave Quickstart](https://colab.research.google.com/drive/1TwlhvvoWIHKDtRUu6eW0NMRq0GzGZ9oX)
28+
- [Weave Board Quickstart: Generative AI Playground](https://colab.research.google.com/drive/1gcR-ucIgjDbDEBFykEpJ3kkBoBQ84Ipr)

weave/examples/agents/explorer.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import json
2+
import subprocess
3+
4+
import weave
5+
from weave.agent.agent import Agent, AgentState
6+
7+
SYSTEM_MESSAGE = """Assistant is an automonmous agent.
8+
Agent likes to explore it's world, using any means necessary.
9+
Agent never stops exploring, it is completely autonomous.
10+
Agent documents its learnings in a file call "journal.txt".
11+
"""
12+
13+
LENGTH_LIMIT = 1000
14+
15+
16+
@weave.op
17+
def run_command(command: str) -> str:
18+
"""Run a shell command and return its output.
19+
20+
Args:
21+
command: The command to run.
22+
23+
Returns:
24+
The output of the command.
25+
"""
26+
try:
27+
completed_process = subprocess.run(
28+
command,
29+
capture_output=True,
30+
text=True,
31+
shell=True,
32+
)
33+
exit_code = completed_process.returncode
34+
stdout = completed_process.stdout.strip()
35+
stderr = completed_process.stderr.strip()
36+
except Exception as e:
37+
exit_code = -1
38+
stdout = ""
39+
stderr = str(e)
40+
41+
if len(stdout) > LENGTH_LIMIT:
42+
stdout = stdout[:LENGTH_LIMIT]
43+
stdout += "\n... (truncated)"
44+
if len(stderr) > LENGTH_LIMIT:
45+
stderr = stderr[:LENGTH_LIMIT]
46+
stderr += "\n... (truncated)"
47+
48+
return json.dumps({"exit_code": exit_code, "stdout": stdout, "stderr": stderr})
49+
50+
51+
if __name__ == "__main__":
52+
# weave.init("wf-explorer1")
53+
54+
agent = Agent(
55+
model_name="gpt-4-0125-preview",
56+
system_message=SYSTEM_MESSAGE,
57+
tools=[run_command],
58+
)
59+
60+
state = AgentState(
61+
history=[
62+
{
63+
"role": "user",
64+
"content": "Explore everything you can find, using your tools. You are completely autonomous, so don't stop to ask for input, just keep exploring.",
65+
},
66+
]
67+
)
68+
69+
while True:
70+
state = agent.step(state)
71+
last_message = state.history[-1]
72+
if last_message["role"] == "assistant" and "tool_calls" not in last_message:
73+
user_input = input("User input: ")
74+
state = AgentState(
75+
history=state.history
76+
+ [
77+
{
78+
"role": "user",
79+
"content": user_input,
80+
}
81+
]
82+
)
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import subprocess
2+
import sys
3+
4+
from rich import print
5+
from rich.console import Console
6+
7+
import weave
8+
from weave.agent.agent import Agent, AgentState
9+
10+
WELCOME = """
11+
Welcome to programmer.
12+
13+
What would you like to do?
14+
15+
"""
16+
17+
SYSTEM_MESSAGE = """Assistant is a programming assistant named "programmer".
18+
programmer is fairly autonomous, and tries to proceed on its own to solve problems.
19+
programmer always has access to a shell and local filesystem in perform tasks, via the run_command tool.
20+
programmer is managed by the user. The user will provide guidance and feedback.
21+
programmer's goal is contained in the spec.txt file.
22+
programmer writes files using the provided tools.
23+
"""
24+
25+
console = Console()
26+
27+
import json
28+
import os
29+
30+
LENGTH_LIMIT = 1000
31+
32+
33+
@weave.op
34+
def list_files(directory: str) -> str:
35+
"""List names of all files in a directory.
36+
37+
Args:
38+
directory: The directory to list.
39+
40+
Returns:
41+
The list of files in the directory.
42+
"""
43+
try:
44+
result = json.dumps(os.listdir(directory))
45+
if len(result) > LENGTH_LIMIT:
46+
result = result[:LENGTH_LIMIT]
47+
result += "\n... (truncated)"
48+
except Exception as e:
49+
return json.dumps([str(e)])
50+
else:
51+
return result
52+
53+
54+
@weave.op
55+
def write_to_file(path: str, content: str) -> str:
56+
"""Write text to a file at the tiven path.
57+
58+
Args:
59+
path: The path to the file.
60+
content: The content to write to the file.
61+
62+
Returns:
63+
A message indicating whether the file was written successfully.
64+
"""
65+
try:
66+
with open(path, "w") as f:
67+
f.write(content)
68+
except Exception as e:
69+
return str(e)
70+
else:
71+
return "File written successfully."
72+
73+
74+
@weave.op
75+
def read_from_file(path: str) -> str:
76+
"""Read text from a file at the given path.
77+
78+
Args:
79+
path: The path to the file.
80+
81+
Returns:
82+
The content of the file.
83+
"""
84+
try:
85+
with open(path) as f:
86+
result = f.read()
87+
if len(result) > LENGTH_LIMIT:
88+
result = result[:LENGTH_LIMIT]
89+
result += "\n... (truncated)"
90+
return result
91+
except Exception as e:
92+
return str(e)
93+
94+
95+
@weave.op
96+
def run_command(command: str) -> str:
97+
"""Run a shell command and return its output.
98+
99+
Args:
100+
command: The command to run.
101+
102+
Returns:
103+
The output of the command.
104+
"""
105+
try:
106+
completed_process = subprocess.run(
107+
command,
108+
capture_output=True,
109+
text=True,
110+
shell=True,
111+
)
112+
exit_code = completed_process.returncode
113+
stdout = completed_process.stdout.strip()
114+
stderr = completed_process.stderr.strip()
115+
except Exception as e:
116+
exit_code = -1
117+
stdout = ""
118+
stderr = str(e)
119+
120+
if len(stdout) > LENGTH_LIMIT:
121+
stdout = stdout[:LENGTH_LIMIT]
122+
stdout += "\n... (truncated)"
123+
if len(stderr) > LENGTH_LIMIT:
124+
stderr = stderr[:LENGTH_LIMIT]
125+
stderr += "\n... (truncated)"
126+
127+
result = f"Exit code: {exit_code}\n"
128+
if stderr:
129+
result += f"STDERR\n{stderr}\n"
130+
if stdout:
131+
result += f"STDOUT\n{stdout}\n"
132+
return result
133+
134+
135+
@weave.op
136+
def run(state: AgentState):
137+
while True:
138+
state = agent.step(state)
139+
last_message = state.history[-1]
140+
if last_message["role"] == "assistant" and "tool_calls" not in last_message:
141+
user_input = input("User input: ")
142+
state = AgentState(
143+
history=state.history
144+
+ [
145+
{
146+
"role": "user",
147+
"content": user_input,
148+
}
149+
]
150+
)
151+
152+
153+
if __name__ == "__main__":
154+
weave.init("wfchobj-programmer2")
155+
console.rule("[bold blue]Programmer")
156+
console.print(WELCOME)
157+
158+
agent = Agent(
159+
model_name="gpt-4-0125-preview",
160+
system_message=SYSTEM_MESSAGE,
161+
tools=[list_files, write_to_file, read_from_file, run_command],
162+
)
163+
164+
if len(sys.argv) < 2:
165+
initial_prompt = input("Initial prompt: ")
166+
else:
167+
initial_prompt = " ".join(sys.argv[1:])
168+
print("Initial prompt:", initial_prompt)
169+
170+
state = AgentState(
171+
history=[
172+
{
173+
"role": "user",
174+
"content": initial_prompt,
175+
},
176+
]
177+
)
178+
179+
run(state)

0 commit comments

Comments
 (0)