Skip to content

Commit

Permalink
update mongodb basic embedding example (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjibc authored Feb 22, 2024
1 parent d8196fa commit 661261f
Show file tree
Hide file tree
Showing 3 changed files with 463 additions and 131 deletions.
135 changes: 68 additions & 67 deletions examples/function_calling/fireworks_langchain_tool_usage.ipynb
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/fw-ai/cookbook/blob/main/examples/function_calling/fireworks_langchain_tool_usage.ipynb)"
],
"metadata": {
"id": "XR-OOx5vQbsA"
}
},
"source": [
"[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/fw-ai/cookbook/blob/main/examples/function_calling/fireworks_langchain_tool_usage.ipynb)"
]
},
{
"cell_type": "code",
Expand All @@ -36,6 +22,9 @@
},
{
"cell_type": "markdown",
"metadata": {
"id": "HSmb-rejDOnT"
},
"source": [
"# Introduction - Fireworks x Langchain\n",
"\n",
Expand All @@ -50,13 +39,13 @@
"For this notebook we are going to use [LangChain](https://www.langchain.com/) framework to construct an agent chain which is capable of chit chatting & solving math equations using a calculator tool.\n",
"\n",
"This agent chain will take in [custom defined tools](https://python.langchain.com/docs/modules/agents/tools/custom_tools) which are capable of executing a Math Query using a LLM. The main routing LLM is going to be Fireworks function calling model."
],
"metadata": {
"id": "HSmb-rejDOnT"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "pYRSabqIL35i"
},
"source": [
"## Setup Deps\n",
"\n",
Expand All @@ -67,13 +56,15 @@
"In order to use the Fireworks AI function calling model, you must first obtain Fireworks API Keys. If you don't already have one, you can one by following the instructions [here](https://readme.fireworks.ai/docs/quickstart). Replace `YOUR_FW_API_KEY` with your obtained key.\n",
"\n",
"**NOTE:** It's important to set temperature to 0.0 for the function calling model because we want reliable behaviour in routing."
],
"metadata": {
"id": "pYRSabqIL35i"
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "rVndeZE_eMFi"
},
"outputs": [],
"source": [
"import os\n",
"\n",
Expand Down Expand Up @@ -104,29 +95,30 @@
" model=\"accounts/fireworks/models/mixtral-8x7b-instruct\",\n",
" temperature=0.0,\n",
")"
],
"metadata": {
"id": "rVndeZE_eMFi"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EGjCfWemNsse"
},
"source": [
"## Custom Tools\n",
"\n",
"In order to seamlessly use function calling ability of the models, we can use [Custom Tools](https://python.langchain.com/docs/modules/agents/tools/custom_tools) functionality built into LangChain. This allows us to seamlessly define the schema of the functions our model can access along with the execution logic. It simplifies the JSON function specification construction.\n",
"\n",
"For this notebook, we are going to construct a `CustomCalculatorTool` which will use LLM to answer & execute math queries. It takes in a single parameter `query` which has to be valid mathemetical expression."
],
"metadata": {
"id": "EGjCfWemNsse"
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "50g_NBE_Atn5"
},
"outputs": [],
"source": [
"\n",
"class CalculatorInput(BaseModel):\n",
" query: str = Field(description=\"should be a math expression\")\n",
"\n",
Expand All @@ -150,43 +142,35 @@
"agent = create_openai_tools_agent(llm, tools, prompt)\n",
"\n",
"agent = AgentExecutor(agent=agent, tools=tools, verbose=True)"
],
"metadata": {
"id": "50g_NBE_Atn5"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "n2FKMb9gORy-"
},
"source": [
"### Test Chit Chat Ability\n",
"\n",
"As we outlined in the beginning, the model should be able to both chit-chat, route queries to external tools when necessary or answer from internal knowledge.\n",
"\n",
"Let's first start with a question that can be answered from internal knowledge."
],
"metadata": {
"id": "n2FKMb9gORy-"
}
]
},
{
"cell_type": "code",
"source": [
"print(agent.invoke({\"input\": \"What is the capital of USA?\"}))"
],
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "A18JJlKaOgLL",
"outputId": "5f0ce2fe-b2ad-4404-8394-f825fd123d71"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
Expand All @@ -197,36 +181,36 @@
"{'input': 'What is the capital of USA?', 'output': 'The capital of USA is Washington, D.C. '}\n"
]
}
],
"source": [
"print(agent.invoke({\"input\": \"What is the capital of USA?\"}))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jL0hyeYCOikE"
},
"source": [
"## Test Calculator\n",
"\n",
"Now let's test it's ability to detect a mathematical question & route the query accordingly."
],
"metadata": {
"id": "jL0hyeYCOikE"
}
]
},
{
"cell_type": "code",
"source": [
"print(agent.invoke({\"input\": \"What is 100 divided by 25?\"}))"
],
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "qu0HFN35OuRo",
"outputId": "89d47217-4b03-4830-add7-03b8b32b61a2"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
Expand All @@ -242,16 +226,16 @@
]
},
{
"output_type": "stream",
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.10/dist-packages/langchain/chains/llm_math/base.py:57: UserWarning: Directly instantiating an LLMMathChain with an llm is deprecated. Please instantiate with llm_chain argument or using the from_llm class method.\n",
" warnings.warn(\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[32;1m\u001b[1;3m```text\n",
"100 / 25\n",
Expand All @@ -266,18 +250,35 @@
"{'input': 'What is 100 divided by 25?', 'output': 'The answer is 4.0. '}\n"
]
}
],
"source": [
"print(agent.invoke({\"input\": \"What is 100 divided by 25?\"}))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XFrp6ckCOyYt"
},
"source": [
"# Conclusion\n",
"\n",
"The fireworks function calling model can route request to external tools or internal knowledge appropriately - thus helping developers build co-operative agents."
],
"metadata": {
"id": "XFrp6ckCOyYt"
}
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
]
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading

0 comments on commit 661261f

Please sign in to comment.