-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathreact_using_mellea.py
More file actions
55 lines (41 loc) · 1.43 KB
/
react_using_mellea.py
File metadata and controls
55 lines (41 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# pytest: ollama, e2e, qualitative, slow
"""React examples using the Mellea library's framework."""
import asyncio
import pydantic
from langchain_community.tools import DuckDuckGoSearchResults
from mellea.backends.tools import MelleaTool
from mellea.stdlib.context import ChatContext
from mellea.stdlib.frameworks.react import react
from mellea.stdlib.session import start_session
m = start_session()
# Simple tool for searching. Requires the langchain-community package.
# Mellea allows you to interop with langchain defined tools.
lc_ddg_search = DuckDuckGoSearchResults(output_format="list")
search_tool = MelleaTool.from_langchain(lc_ddg_search)
class Email(pydantic.BaseModel):
"""An email."""
to: str
subject: str
body: str
async def main():
"""Example."""
# Simple version that just searches for an answer.
out, _ = await react(
goal="What is the Mellea python library?",
context=ChatContext(),
backend=m.backend,
tools=[search_tool],
loop_budget=12,
)
print(out)
# Version that looks up info and formats the final response as an Email object.
# out, _ = await react(
# goal="Write an email about the Mellea python library to Jake with the subject 'cool library'.",
# context=ChatContext(),
# backend=m.backend,
# tools=[search_tool],
# format=Email,
# loop_budget=20,
# )
# print(out)
asyncio.run(main())