DSPy: The framework for programming—not prompting—language models
Python
Rewrite history
Herumb Shandilya998d2c77c039
diff --git a/dspy/predict/avatar/avatar.py b/dspy/predict/avatar/avatar.pyindex 4028681dd..9516e2a49 100644--- a/dspy/predict/avatar/avatar.py+++ b/dspy/predict/avatar/avatar.py@@ -1,6 +1,6 @@import dspy-from copy import deepcopy+from copy import deepcopyfrom pydantic.fields import FieldInfofrom dspy.predict.avatar.signatures import Actordiff --git a/dspy/predict/avatar/models.py b/dspy/predict/avatar/models.pyindex 0d15ef39f..f26642a9d 100644--- a/dspy/predict/avatar/models.py+++ b/dspy/predict/avatar/models.py@@ -16,4 +16,4 @@ class Tool(BaseModel):class Action(BaseModel):tool_name: Any = Field(..., description="Name of the tool to use.")- tool_input_query: Any = Field(..., description="Query to pass as input to the tool.")\ No newline at end of file+ tool_input_query: Any = Field(..., description="Query to pass as input to the tool.")diff --git a/dspy/predict/avatar/signatures.py b/dspy/predict/avatar/signatures.pyindex c61efbc9e..e47dd2a3c 100644--- a/dspy/predict/avatar/signatures.py+++ b/dspy/predict/avatar/signatures.py@@ -1,5 +1,4 @@import dspy-from dspy.predict.avatar.models import Action@@ -19,4 +18,4 @@ You will output action needed to accomplish the `Goal`. `Action` should have a taction_1: Action = dspy.OutputField(prefix="Action 1:",desc="1st action to take.",- )\ No newline at end of file+ )diff --git a/examples/agents/avatar_langchain_tools.ipynb b/examples/agents/avatar_langchain_tools.ipynbnew file mode 100644index 000000000..a82951139--- /dev/null+++ b/examples/agents/avatar_langchain_tools.ipynb@@ -0,0 +1,164 @@+{+ "cells": [+ {+ "cell_type": "code",+ "execution_count": 12,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "env: SERPER_API_KEY=YOUR_SERPER_API_KEY\n",+ "env: OPENAI_API_KEY=YOUR_OPENAI_API_KEY\n"+ ]+ }+ ],+ "source": [+ "%env SERPER_API_KEY=YOUR_SERPER_API_KEY\n",+ "%env OPENAI_API_KEY=YOUR_OPENAI_API_KEY"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 5,+ "metadata": {},+ "outputs": [],+ "source": [+ "import os\n",+ "import dspy\n",+ "\n",+ "from dspy.predict.avatar import Avatar\n",+ "\n",+ "dspy.settings.configure(\n",+ " lm=dspy.OpenAI(\n",+ " model=\"gpt-3.5-turbo-0125\",\n",+ " api_key=os.getenv(\"OPENAI_API_KEY\"),\n",+ " max_tokens=4000,\n",+ " )\n",+ ")\n",+ "\n",+ "class QASignature(dspy.Signature):\n",+ " \"\"\"You will be given a question. Your task is to answer the question.\"\"\"\n",+ " \n",+ " question: str = dspy.InputField(\n",+ " prefix=\"Question:\",\n",+ " desc=\"question to ask\",\n",+ " format=lambda x: x.strip(),\n",+ " )\n",+ " answer: str = dspy.OutputField(\n",+ " prefix=\"Answer:\",\n",+ " desc=\"answer to the question\",\n",+ " )"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 8,+ "metadata": {},+ "outputs": [],+ "source": [+ "from dspy.predict.avatar import Tool\n",+ "from langchain_community.utilities import GoogleSerperAPIWrapper, ArxivAPIWrapper\n",+ "\n",+ "tools = [\n",+ " Tool(\n",+ " tool=GoogleSerperAPIWrapper(),\n",+ " name=\"WEB_SEARCH\",\n",+ " desc=\"Search the web for information to help answer the question.\",\n",+ " ),\n",+ " Tool(\n",+ " tool=ArxivAPIWrapper(),\n",+ " name=\"ARXIV_SEARCH\",\n",+ " desc=\"Search the doi through arxiv to get the paper.\"\n",+ " ),\n",+ "]\n",+ "\n",+ "avatar = Avatar(\n",+ " QASignature,\n",+ " tools=tools,\n",+ " verbose=True,\n",+ ")"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 9,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Starting the task...\n",+ "Action 1: WEB_SEARCH (History of the internet)\n",+ "Action 2: Finish (No additional input required)\n",+ "\n",+ "\n",+ "Answer:\n",+ "The Internet started in the 1960s as a way for government researchers to share information. It has its origins in the efforts of scientists and engineers to build and interconnect computer networks.\n"+ ]+ }+ ],+ "source": [+ "answer = avatar(question=\"Give me a brief overview of the history of the internet.\").answer\n",+ "\n",+ "print(f\"\\n\\nAnswer:\\n{answer}\")"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 10,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Starting the task...\n",+ "Action 1: ARXIV_SEARCH (2201.00432)\n",+ "Action 2: WEB_SEARCH (titles related to independence polynomials of trees)\n",+ "Action 3: Finish (No further action needed.)\n",+ "\n",+ "\n",+ "Answer:\n",+ "The paper 2201.00432 is titled \"A Linear Algorithm for Computing Independence Polynomials of Trees\" by Ohr Kadrawi, Vadim E. Levit, Ron Yosef, Matan Mizrachi. The paper discusses an efficient algorithm for computing the independence polynomials of trees in linear time without the need for a database. Related papers on the topic include \"On Unimodality of Independence Polynomials of Trees\" and \"An identity for the independence polynomials of trees\" by Ivan Gutman.\n"+ ]+ }+ ],+ "source": [+ "answer = avatar(question=\"Summarize 2201.00432 for me and share titles for related papers on web.\").answer\n",+ " \n",+ "print(f\"\\n\\nAnswer:\\n{answer}\")"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": []+ }+ ],+ "metadata": {+ "kernelspec": {+ "display_name": "dspy-74wouE_3-py3.10",+ "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.10.12"+ }+ },+ "nbformat": 4,+ "nbformat_minor": 2+}