Skip to content

Commit

Permalink
update notebooks (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
TengHu authored Jan 6, 2024
1 parent 52460e7 commit 80f7c7b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 29 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ response = openai_client.chat.completions.create(
)
```

### Easily integrate tools from libraries such as [Langchain](https://github.com/langchain-ai/langchain/tree/master/libs/community)

```python
from actionweaver.actions.factories.langchain import action_from_tool

from langchain_community.tools.google_search.tool import GoogleSearchRun
from langchain_community.utilities.google_search import GoogleSearchAPIWrapper

search_tool = GoogleSearchRun(api_wrapper=GoogleSearchAPIWrapper())

openai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "what date is today?"}],
actions = [action_from_tool(search_tool)]
)
```

### Force execution of an action
You can also force the language model to execute the action.
```python
Expand Down
85 changes: 66 additions & 19 deletions docs/source/notebooks/cookbooks/quickstart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@
"metadata": {},
"outputs": [],
"source": [
"from actionweaver.llms.openai.tools.tokens import TokenUsageTracker\n",
"from actionweaver.utils.tokens import TokenUsageTracker\n",
"from actionweaver.llms import patch\n",
"from openai import OpenAI\n",
"\n",
"openai_client = patch(OpenAI())\n",
"\n"
"openai_client = patch(OpenAI())"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 9,
"id": "22710e02-f81f-4050-8c2c-01cd64e48f32",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -79,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 10,
"id": "69c84261-b03b-4333-8cea-8315241c69a5",
"metadata": {},
"outputs": [
Expand All @@ -94,10 +93,10 @@
{
"data": {
"text/plain": [
"ChatCompletion(id='chatcmpl-8asF0WseqNkjLTsoTjUTRi4wPR7gO', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The current temperature in San Francisco is 72°F. However, I am unable to retrieve the current temperature for Beijing at the moment.', role='assistant', function_call=None, tool_calls=None))], created=1703799002, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=28, prompt_tokens=155, total_tokens=183))"
"ChatCompletion(id='chatcmpl-8e9y8FGK2m2srSbp4Bjk3dqBpSChZ', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content=\"The current weather in San Francisco is 72°F. However, I couldn't find the current weather for Beijing.\", role='assistant', function_call=None, tool_calls=None))], created=1704582132, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=24, prompt_tokens=155, total_tokens=179))"
]
},
"execution_count": 4,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -120,14 +119,6 @@
"response"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d0690972-4b61-48b5-9b3f-a21c3bb2df5d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "92e1741f-3e0a-4673-9e2d-683d8e51ff47",
Expand All @@ -138,7 +129,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 11,
"id": "f12f7870-aa76-4755-ab89-96737ca9a790",
"metadata": {},
"outputs": [],
Expand All @@ -155,7 +146,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 12,
"id": "f342800e-fd6e-4195-9da9-44a4cc713f65",
"metadata": {},
"outputs": [
Expand All @@ -170,10 +161,10 @@
{
"data": {
"text/plain": [
"ChatCompletion(id='chatcmpl-8asF8SNGGczPhuQnTY6WP7IjY6KJt', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The current weather in San Francisco is 72°F. However, the current weather in Beijing is currently unknown.', role='assistant', function_call=None, tool_calls=None), content_filter_results={'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}})], created=1703799010, model='gpt-35-turbo-16k', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=23, prompt_tokens=155, total_tokens=178), prompt_filter_results=[{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}])"
"ChatCompletion(id='chatcmpl-8e9yKb941WGZbspsGSPjS5TzrQQs7', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content=\"The weather in San Francisco is currently 72°F. However, I'm sorry, but I don't have the current weather information for Beijing.\", role='assistant', function_call=None, tool_calls=None), content_filter_results={'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}})], created=1704582144, model='gpt-35-turbo-16k', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=30, prompt_tokens=155, total_tokens=185), prompt_filter_results=[{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}])"
]
},
"execution_count": 7,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -196,6 +187,62 @@
"response"
]
},
{
"cell_type": "markdown",
"id": "d70eb220-807d-41f3-afa8-5d1c6305c6cb",
"metadata": {},
"source": [
"**Easily integrate tools from libraries such as [Langchain](https://github.com/langchain-ai/langchain/tree/master/libs/community)**"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "2b6569a0-c2e1-4109-b88a-8025ed76dfab",
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.tools.google_search.tool import GoogleSearchRun\n",
"from langchain_community.utilities.google_search import GoogleSearchAPIWrapper\n",
"search_tool = GoogleSearchRun(api_wrapper=GoogleSearchAPIWrapper())"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "568e6e30-362b-4ab6-a088-99370269c205",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ChatCompletion(id='chatcmpl-8eA05byBaqO8ej5eEPmEKKeHHRPAZ', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The weather information for San Francisco and Tokyo is not provided in the search results. However, you can use weather websites or apps to check the current weather conditions and forecast for both cities.', role='assistant', function_call=None, tool_calls=None), content_filter_results={'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}})], created=1704582253, model='gpt-35-turbo-16k', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=38, prompt_tokens=774, total_tokens=812), prompt_filter_results=[{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": \"what's the weather in San Francisco and Tokyo ?\"}\n",
" ]\n",
"\n",
"from actionweaver.actions.factories.langchain import action_from_tool\n",
"\n",
"response = azure_client.chat.completions.create(\n",
" model=\"gpt-35-turbo-0613-16k\",\n",
" messages=messages,\n",
" stream=False,\n",
" actions = [action_from_tool(search_tool)],\n",
" token_usage_tracker = TokenUsageTracker(500),\n",
")\n",
"\n",
"response"
]
},
{
"cell_type": "markdown",
"id": "b04d6414-c04c-4fbe-bfeb-8cb9effc2b39",
Expand Down
35 changes: 25 additions & 10 deletions docs/source/notebooks/cookbooks/stateful_agent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
{
"data": {
"text/plain": [
"ChatCompletion(id='chatcmpl-8cigjADM3wmMGnKhATp69Iojk41ZY', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The current time is 18:42:16.', role='assistant', function_call=None, tool_calls=None))], created=1704238937, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=12, prompt_tokens=100, total_tokens=112))"
"ChatCompletion(id='chatcmpl-8e9w3bflg5iwHCenO7WGjSVZVSyvT', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The current time is 2024-01-06 18:00:02.', role='assistant', function_call=None, tool_calls=None))], created=1704582003, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=19, prompt_tokens=100, total_tokens=119))"
]
},
"execution_count": 3,
Expand All @@ -96,29 +96,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "842e1792-3210-4be8-85c9-f6c669dfe127",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"ChatCompletion(id='chatcmpl-8e9wfhRvZ56jZc2PxU4Fd6d7Vy6jM', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content=\"Today's events include:\\n\\n1. Perugino: Adoration of the Magi - A featured event celebrating Epiphany, a major feast, held annually on this day.\\n\\n2. Legislative proceedings - Various legislative sessions and motions are taking place today in the Assembly and Senate.\\n\\n3. New York State Fair - There are numerous fun activities and events happening at the New York State Fair. You can plan your itinerary by selecting the events you are interested in.\\n\\n4. Senate events - You can watch or listen to today's Senate events, as well as access the TV schedule, media archive, and more.\\n\\n5. Campus Recreation - There is a Judo Club meeting at the UNL Coliseum in Temporary Conference Room 306.\\n\\n6. Apple Shortcut - There is a suggestion to use an Apple Shortcut to get a list of today's Calendar Events and put them into a Keyboard.\\n\\nPlease note that these are just a few highlights, and there may be additional events happening today.\", role='assistant', function_call=None, tool_calls=None))], created=1704582041, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=197, prompt_tokens=428, total_tokens=625))"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class LangChainTools:\n",
" def __init__(self):\n",
" from langchain_community.tools.google_search.tool import GoogleSearchRun\n",
" from langchain_community.utilities.google_search import GoogleSearchAPIWrapper\n",
" self.google_search_api = GoogleSearchRun(api_wrapper=GoogleSearchAPIWrapper())\n",
" \n",
" @action(name=\"GoogleSearch\")\n",
" def google_search(self, query: str) -> str:\n",
" \"\"\"\n",
" Perform a Google search using the provided query. \n",
" \n",
" This action requires `langchain` and `google-api-python-client` installed, and GOOGLE_API_KEY, GOOGLE_CSE_ID environment variables.\n",
" See https://python.langchain.com/docs/integrations/tools/google_search.\n",
"\n",
" :param query: The search query to be used for the Google search.\n",
" :return: The search results as a string.\n",
" \"\"\"\n",
" from langchain.utilities import GoogleSearchAPIWrapper\n",
"\n",
" search = GoogleSearchAPIWrapper()\n",
" return search.run(query)\n",
" return self.google_search_api(query)\n",
" \n",
"class AgentV1(AgentV0, LangChainTools):\n",
" def __init__(self):\n",
" AgentV0.__init__(self)\n",
" LangChainTools.__init__(self)\n",
" \n",
" \n",
" def __call__(self, text):\n",
" self.messages += [{\"role\": \"user\", \"content\":text}]\n",
" return self.llm.chat.completions.create(model=\"gpt-3.5-turbo\", messages=self.messages, actions = [self.google_search])\n",
Expand Down

0 comments on commit 80f7c7b

Please sign in to comment.