mirror of
https://github.com/hwchase17/langchain
synced 2024-10-31 15:20:26 +00:00
2667ddc686
**Description: a description of the change** Fixed `make docs_build` and related scripts which caused errors. There are several changes. First, I made the build of the documentation and the API Reference into two separate commands. This is because it takes less time to build. The commands for documents are `make docs_build`, `make docs_clean`, and `make docs_linkcheck`. The commands for API Reference are `make api_docs_build`, `api_docs_clean`, and `api_docs_linkcheck`. It looked like `docs/.local_build.sh` could be used to build the documentation, so I used that. Since `.local_build.sh` was also building API Rerefence internally, I removed that process. `.local_build.sh` also added some Bash options to stop in error or so. Futher more added `cd "${SCRIPT_DIR}"` at the beginning so that the script will work no matter which directory it is executed in. `docs/api_reference/api_reference.rst` is removed, because which is generated by `docs/api_reference/create_api_rst.py`, and added it to .gitignore. Finally, the description of CONTRIBUTING.md was modified. **Issue: the issue # it fixes (if applicable)** https://github.com/hwchase17/langchain/issues/6413 **Dependencies: any dependencies required for this change** `nbdoc` was missing in group docs so it was added. I installed it with the `poetry add --group docs nbdoc` command. I am concerned if any modifications are needed to poetry.lock. I would greatly appreciate it if you could pay close attention to this file during the review. **Tag maintainer** - General / Misc / if you don't know who to tag: @baskaryan If this PR needs any additional changes, I'll be happy to make them! --------- Co-authored-by: Bagatur <baskaryan@gmail.com>
387 lines
11 KiB
Plaintext
387 lines
11 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "g9EmNu5DD9YI"
|
|
},
|
|
"source": [
|
|
"# Custom functions with OpenAI Functions Agent\n",
|
|
"\n",
|
|
"This notebook goes through how to integrate custom functions with OpenAI Functions agent."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "LFKylC3CPtTl"
|
|
},
|
|
"source": [
|
|
"Install libraries which are required to run this example notebook\n",
|
|
"\n",
|
|
"`pip install -q openai langchain yfinance`"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "E2DqzmEGDPak"
|
|
},
|
|
"source": [
|
|
"## Define custom functions"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"id": "SiucthMs6SIK"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import yfinance as yf\n",
|
|
"from datetime import datetime, timedelta\n",
|
|
"\n",
|
|
"\n",
|
|
"def get_current_stock_price(ticker):\n",
|
|
" \"\"\"Method to get current stock price\"\"\"\n",
|
|
"\n",
|
|
" ticker_data = yf.Ticker(ticker)\n",
|
|
" recent = ticker_data.history(period=\"1d\")\n",
|
|
" return {\"price\": recent.iloc[0][\"Close\"], \"currency\": ticker_data.info[\"currency\"]}\n",
|
|
"\n",
|
|
"\n",
|
|
"def get_stock_performance(ticker, days):\n",
|
|
" \"\"\"Method to get stock price change in percentage\"\"\"\n",
|
|
"\n",
|
|
" past_date = datetime.today() - timedelta(days=days)\n",
|
|
" ticker_data = yf.Ticker(ticker)\n",
|
|
" history = ticker_data.history(start=past_date)\n",
|
|
" old_price = history.iloc[0][\"Close\"]\n",
|
|
" current_price = history.iloc[-1][\"Close\"]\n",
|
|
" return {\"percent_change\": ((current_price - old_price) / old_price) * 100}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "vRLINGvQR1rO",
|
|
"outputId": "68230a4b-dda2-4273-b956-7439661e3785"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'price': 334.57000732421875, 'currency': 'USD'}"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"get_current_stock_price(\"MSFT\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "57T190q235mD",
|
|
"outputId": "c6ee66ec-0659-4632-85d1-263b08826e68"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'percent_change': 1.014466941163018}"
|
|
]
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"get_stock_performance(\"MSFT\", 30)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "MT8QsdyBDhwg"
|
|
},
|
|
"source": [
|
|
"## Make custom tools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {
|
|
"id": "NvLOUv-XP3Ap"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from typing import Type\n",
|
|
"from pydantic import BaseModel, Field\n",
|
|
"from langchain.tools import BaseTool\n",
|
|
"\n",
|
|
"\n",
|
|
"class CurrentStockPriceInput(BaseModel):\n",
|
|
" \"\"\"Inputs for get_current_stock_price\"\"\"\n",
|
|
"\n",
|
|
" ticker: str = Field(description=\"Ticker symbol of the stock\")\n",
|
|
"\n",
|
|
"\n",
|
|
"class CurrentStockPriceTool(BaseTool):\n",
|
|
" name = \"get_current_stock_price\"\n",
|
|
" description = \"\"\"\n",
|
|
" Useful when you want to get current stock price.\n",
|
|
" You should enter the stock ticker symbol recognized by the yahoo finance\n",
|
|
" \"\"\"\n",
|
|
" args_schema: Type[BaseModel] = CurrentStockPriceInput\n",
|
|
"\n",
|
|
" def _run(self, ticker: str):\n",
|
|
" price_response = get_current_stock_price(ticker)\n",
|
|
" return price_response\n",
|
|
"\n",
|
|
" def _arun(self, ticker: str):\n",
|
|
" raise NotImplementedError(\"get_current_stock_price does not support async\")\n",
|
|
"\n",
|
|
"\n",
|
|
"class StockPercentChangeInput(BaseModel):\n",
|
|
" \"\"\"Inputs for get_stock_performance\"\"\"\n",
|
|
"\n",
|
|
" ticker: str = Field(description=\"Ticker symbol of the stock\")\n",
|
|
" days: int = Field(description=\"Timedelta days to get past date from current date\")\n",
|
|
"\n",
|
|
"\n",
|
|
"class StockPerformanceTool(BaseTool):\n",
|
|
" name = \"get_stock_performance\"\n",
|
|
" description = \"\"\"\n",
|
|
" Useful when you want to check performance of the stock.\n",
|
|
" You should enter the stock ticker symbol recognized by the yahoo finance.\n",
|
|
" You should enter days as number of days from today from which performance needs to be check.\n",
|
|
" output will be the change in the stock price represented as a percentage.\n",
|
|
" \"\"\"\n",
|
|
" args_schema: Type[BaseModel] = StockPercentChangeInput\n",
|
|
"\n",
|
|
" def _run(self, ticker: str, days: int):\n",
|
|
" response = get_stock_performance(ticker, days)\n",
|
|
" return response\n",
|
|
"\n",
|
|
" def _arun(self, ticker: str):\n",
|
|
" raise NotImplementedError(\"get_stock_performance does not support async\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "PVKoqeCyFKHF"
|
|
},
|
|
"source": [
|
|
"## Create Agent"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {
|
|
"id": "yY7qNB7vSQGh"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.agents import AgentType\n",
|
|
"from langchain.chat_models import ChatOpenAI\n",
|
|
"from langchain.agents import initialize_agent\n",
|
|
"\n",
|
|
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0613\", temperature=0)\n",
|
|
"\n",
|
|
"tools = [CurrentStockPriceTool(), StockPerformanceTool()]\n",
|
|
"\n",
|
|
"agent = initialize_agent(tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 321
|
|
},
|
|
"id": "4X96xmgwRkcC",
|
|
"outputId": "a91b13ef-9643-4f60-d067-c4341e0b285e"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new chain...\u001b[0m\n",
|
|
"\u001b[32;1m\u001b[1;3m\n",
|
|
"Invoking: `get_current_stock_price` with `{'ticker': 'MSFT'}`\n",
|
|
"\n",
|
|
"\n",
|
|
"\u001b[0m\u001b[36;1m\u001b[1;3m{'price': 334.57000732421875, 'currency': 'USD'}\u001b[0m\u001b[32;1m\u001b[1;3m\n",
|
|
"Invoking: `get_stock_performance` with `{'ticker': 'MSFT', 'days': 180}`\n",
|
|
"\n",
|
|
"\n",
|
|
"\u001b[0m\u001b[33;1m\u001b[1;3m{'percent_change': 40.163963297187905}\u001b[0m\u001b[32;1m\u001b[1;3mThe current price of Microsoft stock is $334.57 USD. \n",
|
|
"\n",
|
|
"Over the past 6 months, Microsoft stock has performed well with a 40.16% increase in its price.\u001b[0m\n",
|
|
"\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'The current price of Microsoft stock is $334.57 USD. \\n\\nOver the past 6 months, Microsoft stock has performed well with a 40.16% increase in its price.'"
|
|
]
|
|
},
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"agent.run(\n",
|
|
" \"What is the current price of Microsoft stock? How it has performed over past 6 months?\"\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 285
|
|
},
|
|
"id": "nkZ_vmAcT7Al",
|
|
"outputId": "092ebc55-4d28-4a4b-aa2a-98ae47ceec20"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new chain...\u001b[0m\n",
|
|
"\u001b[32;1m\u001b[1;3m\n",
|
|
"Invoking: `get_current_stock_price` with `{'ticker': 'GOOGL'}`\n",
|
|
"\n",
|
|
"\n",
|
|
"\u001b[0m\u001b[36;1m\u001b[1;3m{'price': 118.33000183105469, 'currency': 'USD'}\u001b[0m\u001b[32;1m\u001b[1;3m\n",
|
|
"Invoking: `get_current_stock_price` with `{'ticker': 'META'}`\n",
|
|
"\n",
|
|
"\n",
|
|
"\u001b[0m\u001b[36;1m\u001b[1;3m{'price': 287.04998779296875, 'currency': 'USD'}\u001b[0m\u001b[32;1m\u001b[1;3mThe recent stock price of Google (GOOGL) is $118.33 USD and the recent stock price of Meta (META) is $287.05 USD.\u001b[0m\n",
|
|
"\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'The recent stock price of Google (GOOGL) is $118.33 USD and the recent stock price of Meta (META) is $287.05 USD.'"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"agent.run(\"Give me recent stock prices of Google and Meta?\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 466
|
|
},
|
|
"id": "jLU-HjMq7n1o",
|
|
"outputId": "a42194dd-26ed-4b5a-d4a2-1038420045c4"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new chain...\u001b[0m\n",
|
|
"\u001b[32;1m\u001b[1;3m\n",
|
|
"Invoking: `get_stock_performance` with `{'ticker': 'MSFT', 'days': 90}`\n",
|
|
"\n",
|
|
"\n",
|
|
"\u001b[0m\u001b[33;1m\u001b[1;3m{'percent_change': 18.043096235165596}\u001b[0m\u001b[32;1m\u001b[1;3m\n",
|
|
"Invoking: `get_stock_performance` with `{'ticker': 'GOOGL', 'days': 90}`\n",
|
|
"\n",
|
|
"\n",
|
|
"\u001b[0m\u001b[33;1m\u001b[1;3m{'percent_change': 17.286155760642853}\u001b[0m\u001b[32;1m\u001b[1;3mIn the past 3 months, Microsoft (MSFT) has performed better than Google (GOOGL). Microsoft's stock price has increased by 18.04% while Google's stock price has increased by 17.29%.\u001b[0m\n",
|
|
"\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\"In the past 3 months, Microsoft (MSFT) has performed better than Google (GOOGL). Microsoft's stock price has increased by 18.04% while Google's stock price has increased by 17.29%.\""
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"agent.run(\n",
|
|
" \"In the past 3 months, which stock between Microsoft and Google has performed the best?\"\n",
|
|
")"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"provenance": []
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.9.16"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|