Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mzr1996 committed Dec 14, 2023
1 parent e8034ea commit e126786
Show file tree
Hide file tree
Showing 4 changed files with 629 additions and 13 deletions.
110 changes: 110 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# AgentLego examples

## Lagent

1. Prepare environment.

```bash
pip install lagent
pip install agentlego
```

2. Specify your OpenAI key in environment variables.

```bash
export OPEN_API_KEY=sk-xxx
```

3. Start chatting in terminal.

```bash
python examples/lagent_example.py --model gpt-3.5-turbo --tools Calculator
```

> If your want other tools, install dependencies at first and specify them in the parameters.
```text
User: Please tell me the result of cosine pi/6.
Bot:
Thought: To find the result of cosine pi/6, I can use the calculator tool.
Action: Calculator
Action Input: {"expression": "cos(pi/6)"}
System:Response:0.8660254037844387
Bot:
Final Answer: The result of cosine pi/6 is approximately 0.8660254037844387.
```

## LangChain

1. Prepare environment.

```bash
pip install langchain
pip install agentlego
```

2. Specify your OpenAI key in environment variables.

```bash
export OPEN_API_KEY=sk-xxx
```

3. Start chatting in terminal.

```bash
python examples/langchain_example.py --model gpt-4-1106-preview --tools Calculator
```

> If your want other tools, install dependencies at first and specify them in the parameters.
````text
User: Please tell me the result of cosine pi/6.
> Entering new AgentExecutor chain...
Action:
```
{
"action": "Calculator",
"action_input": {
"expression": "math.cos(math.pi/6)"
}
}
```
Observation: 0.8660254037844387
Thought:Action:
```
{
"action": "Final Answer",
"action_input": "The result of cosine pi/6 is approximately 0.8660254037844387."
}
```
> Finished chain.
gpt-4-1106-preview: The result of cosine pi/6 is approximately 0.8660254037844387.
````

## Streamlit demo

1. Prepare environment.

```bash
pip install lagent
pip install agentlego
pip install streamlit==1.29.0
```

2. Specify your OpenAI key in environment variables.

```bash
export OPEN_API_KEY=sk-xxx
```

3. Start streamlit demo.

```bash
streamlit run examples/lagent_example.py -- --tools Calculator
```

> If your want other tools, install dependencies at first and specify them in the parameters.
13 changes: 7 additions & 6 deletions examples/lagent_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default='gpt-3.5-turbo')
parser.add_argument(
'--tools',
type=str,
nargs='+',
default=['GoogleSearch', 'Calculator'],
)
args = parser.parse_args()
return args

Expand All @@ -18,12 +24,7 @@ def main():

# set OPEN_API_KEY in your environment or directly pass it with key=''
model = GPTAPI(model_type=args.model)
tools = [
load_tool(tool_type).to_lagent() for tool_type in [
'GoogleSearch',
'Calculator',
]
]
tools = [load_tool(tool_type).to_lagent() for tool_type in args.tools]
chatbot = ReAct(
llm=model,
max_turn=3,
Expand Down
14 changes: 7 additions & 7 deletions examples/langchain_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default='gpt-3.5-turbo')
parser.add_argument(
'--tools',
type=str,
nargs='+',
default=['GoogleSearch', 'Calculator'],
)
args = parser.parse_args()
return args


def main():
args = parse_args()

tools = [
load_tool(tool_type).to_langchain() for tool_type in [
'Calculator',
'GoogleSearch',
'ImageCaption',
]
]
tools = [load_tool(tool_type).to_langchain() for tool_type in args.tools]
# set OPEN_API_KEY in your environment or directly pass it with key=''
llm = ChatOpenAI(temperature=0, model=args.model)
memory = ConversationBufferMemory(
Expand Down
Loading

0 comments on commit e126786

Please sign in to comment.