From 4aed6395c38564525aee88b3ffd71fd013d81b86 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Thu, 17 Aug 2023 11:14:15 -0700 Subject: [PATCH] cr --- examples/web-research.ipynb | 164 ++++++++++++++++-------------------- 1 file changed, 74 insertions(+), 90 deletions(-) diff --git a/examples/web-research.ipynb b/examples/web-research.ipynb index 8e35a17f0..b1e26c7a9 100644 --- a/examples/web-research.ipynb +++ b/examples/web-research.ipynb @@ -50,40 +50,31 @@ { "cell_type": "code", "execution_count": 3, - "id": "7db344e2-fac4-4047-a422-6b0df738e656", - "metadata": { - "scrolled": true - }, + "id": "89d4a96a-2f59-491f-81fd-4a5d755c0081", + "metadata": {}, "outputs": [], "source": [ - "# !pip install google-api-python-client\n", - "# !pip install html2text" + "from duckduckgo_search import DDGS\n", + "\n", + "ddgs = DDGS()" ] }, { "cell_type": "code", "execution_count": 4, - "id": "25983034-a563-403e-b9d4-400a27e6ec29", - "metadata": {}, - "outputs": [], - "source": [ - "search_tool = GoogleSearchAPIWrapper()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, "id": "dcdc3812-0cdc-4677-bf9d-f9d7d5cac7e2", "metadata": {}, "outputs": [], "source": [ "def retrieve_documents(query):\n", " query=query.strip().strip('\"')\n", + " search_results = ddgs.text(query)\n", " urls_to_look = []\n", - " search_results = search_tool.results(query, 5)\n", " for res in search_results:\n", - " if res.get(\"link\", None):\n", - " urls_to_look.append(res[\"link\"])\n", + " if res.get(\"href\", None):\n", + " urls_to_look.append(res[\"href\"])\n", + " if len(urls_to_look) >= 4:\n", + " break\n", " \n", " # Relevant urls\n", " # Load, split, and add new urls to vectorstore\n", @@ -97,6 +88,17 @@ " return docs" ] }, + { + "cell_type": "code", + "execution_count": 5, + "id": "45bb7661-35db-4a67-a62e-9c791c9de359", + "metadata": {}, + "outputs": [], + "source": [ + "import nest_asyncio\n", + "nest_asyncio.apply()" + ] + }, { "cell_type": "code", "execution_count": 6, @@ -107,27 +109,6 @@ "#docs = retrieve_documents(\"langchain\")" ] }, - { - "cell_type": "markdown", - "id": "d1c7d867-d9b3-4d05-826f-f9d6602c8be6", - "metadata": {}, - "source": [ - "## Querier\n", - "\n", - "We will now come up with an actor to generate a query to search for given a user request" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "f01c5fae-75d3-4107-81e6-9a254b0d50c5", - "metadata": {}, - "outputs": [], - "source": [ - "prompt = ChatPromptTemplate.from_template(\"Come up with a search query given the user question:\\n\\n{question}\")\n", - "query_chain = prompt | ChatOpenAI() | StrOutputParser()" - ] - }, { "cell_type": "markdown", "id": "73e0b79f-f6bd-4f68-9433-645ee1eea81e", @@ -139,7 +120,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 24, "id": "901e1f8d-c973-4998-a731-5dab0c147b8c", "metadata": {}, "outputs": [], @@ -149,12 +130,12 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 25, "id": "915bec33-d210-4471-b051-859ecba608be", "metadata": {}, "outputs": [], "source": [ - "summarizer_chain = prompt | ChatOpenAI().with_fallbacks([ChatAnthropic(model=\"claude-2\")]) | StrOutputParser()" + "summarizer_chain = prompt | ChatOpenAI(max_retries=0).with_fallbacks([ChatOpenAI(model=\"gpt-3.5-turbo-16k\"), ChatAnthropic(model=\"claude-2\")]) | StrOutputParser()" ] }, { @@ -167,43 +148,27 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 26, "id": "edc0def4-d184-4438-9099-e6604ba9ff28", "metadata": {}, "outputs": [], "source": [ - "query_inbox = Topic(\"query\")\n", + "\n", "summarizer_inbox = Topic(\"summarizer\")" ] }, { "cell_type": "code", - "execution_count": 11, - "id": "33540bc4-bb75-4e5a-876f-2b7b842e5509", - "metadata": {}, - "outputs": [], - "source": [ - "query_actor = (\n", - " # Listed in inputs\n", - " Topic.IN.subscribe()\n", - " | query_chain\n", - " # The draft always goes to the editors inbox\n", - " | query_inbox.publish()\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 12, + "execution_count": 27, "id": "23f89611-7b0f-4f01-b9a7-119124e3341d", "metadata": {}, "outputs": [], "source": [ "search_actor = (\n", - " query_inbox.subscribe()\n", + " Topic.IN.subscribe()\n", " | {\n", " \"search_results\": retrieve_documents,\n", - " \"question\": lambda x: x,\n", + " \"question\": Topic.IN.current(),\n", " }\n", " | summarizer_inbox.publish()\n", ")" @@ -211,7 +176,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 28, "id": "2d4a6b51-bd93-47c2-a301-9593a47df7d4", "metadata": {}, "outputs": [], @@ -225,25 +190,25 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 29, "id": "11d3b066-7f95-4ad9-82d0-ba64bbf3e3fa", "metadata": {}, "outputs": [], "source": [ "web_researcher = PubSub(\n", - " processes=(query_actor, search_actor, summ_actor),\n", + " processes=(search_actor, summ_actor),\n", " connection=InMemoryPubSubConnection(),\n", ")" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 30, "id": "bc022d51-69f0-4da9-8025-70afdc3cc6a8", "metadata": {}, "outputs": [], "source": [ - "#web_researcher.invoke({\"question\": \"What is langsmith?\"})" + "#web_researcher.invoke(\"What is langsmith?\")" ] }, { @@ -256,7 +221,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 31, "id": "43ca019d-a500-4c77-8e62-a46e54ffae7d", "metadata": {}, "outputs": [], @@ -266,7 +231,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 32, "id": "858a82ae-a73f-4da2-9210-298af789ea30", "metadata": {}, "outputs": [], @@ -296,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 33, "id": "c0298fdc-0e7c-4e79-9cb7-cdd4d50fe88f", "metadata": {}, "outputs": [ @@ -305,12 +270,12 @@ "text/plain": [ "['What is the purpose of Langsmith?',\n", " 'Who developed Langsmith?',\n", - " 'What are the key features of Langsmith?',\n", + " 'What are the features of Langsmith?',\n", " 'How does Langsmith work?',\n", " 'Are there any alternatives to Langsmith?']" ] }, - "execution_count": 18, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -321,7 +286,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 34, "id": "a0101bd0-cd95-4b13-ab26-d5d34d703bf9", "metadata": {}, "outputs": [], @@ -345,7 +310,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 35, "id": "d7715e0c-23c0-4985-97a7-bf5b151cf734", "metadata": {}, "outputs": [], @@ -356,7 +321,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 36, "id": "f274027a-c9f0-4efa-b798-1921b9b376d9", "metadata": {}, "outputs": [], @@ -371,7 +336,7 @@ "research_actor = (\n", " research_inbox.subscribe()\n", " | {\n", - " \"research\": lambda x: web_researcher.batch([{\"question\": i} for i in x]),\n", + " \"research\": lambda x: web_researcher.batch(x),\n", " #\"research\": lambda x: [web_researcher.invoke({\"question\": i}) for i in x],\n", " \"question\": Topic.IN.current() | itemgetter(\"question\"),\n", " }\n", @@ -386,7 +351,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 37, "id": "789b636d-23ce-44fb-a8e3-fdfbfabe77ff", "metadata": {}, "outputs": [], @@ -399,7 +364,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "id": "8f713a31-5f60-4d90-9b95-3f42d8fbbddb", "metadata": {}, "outputs": [ @@ -407,25 +372,44 @@ "name": "stderr", "output_type": "stream", "text": [ - "Fetching pages: 100%|#####################################################################################################################################################################################| 5/5 [00:01<00:00, 3.27it/s]\n", - "Fetching pages: 0%| | 0/5 [00:00._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo-16k in organization org-i0zjYONU3PemzJ222esBaAzZ on tokens per min. Limit: 180000 / min. Current: 173743 / min. Contact us through our help center at help.openai.com if you continue to have issues..\n", + "Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo-16k in organization org-i0zjYONU3PemzJ222esBaAzZ on tokens per min. Limit: 180000 / min. Current: 161254 / min. Contact us through our help center at help.openai.com if you continue to have issues..\n" ] + }, + { + "data": { + "text/plain": [ + "['Research Report: Understanding LangSmith\\n\\nIntroduction:\\nThe purpose of this research report is to provide a comprehensive understanding of LangSmith, a developer platform designed to facilitate the development and management of Language Model applications (LLMs). Through an analysis of the gathered research, this report aims to answer the question: \"What is LangSmith?\"\\n\\nResearch Findings:\\n\\n1. LangSmith Overview:\\nLangSmith is a unified platform that helps developers trace, evaluate, and monitor LLM applications and intelligent agents. It aims to simplify the process of moving from prototype to production by providing features such as tracing runs, testing prompts or answers, and exporting datasets and runs for further analysis. LangSmith offers comprehensive visibility into the chain sequence of calls, real-time insights, and observability features to monitor LLM applications. It emphasizes best practices and offers useful tools and resources for developers working with LLMs.\\n\\n2. Key Features of LangSmith:\\nLangSmith offers several key features to assist developers in building and managing LLM applications. These include:\\n- Tracing and evaluating the behavior of LLM applications.\\n- Debugging and experimentation capabilities.\\n- Sharing work with others.\\n- Creating datasets for testing and evaluation.\\n- Evaluating models based on created datasets.\\n- Monitoring the behavior and performance of LLM applications.\\n- Comprehensive visibility into the entire chain sequence of calls.\\n\\n3. LangSmith\\'s Integration with LangChain:\\nLangSmith seamlessly integrates with LangChain, a library for prototyping LLM applications. This integration allows developers to leverage the composability of LangChain and build applications with large language models effectively. LangChain supports features such as memory, custom datasets, and more.\\n\\n4. Potential Alternatives to LangSmith:\\nBased on the research findings, several potential alternatives to LangSmith have been identified. These include:\\n- LangChain: An open-source framework for building applications with large language models through composability.\\n- GradientJ: A platform to build, orchestrate, and manage complex LLM applications at scale.\\n- LLMOps.Space: A community and resource hub focused on deploying LLMs into production.\\n- Vellum: A development platform aimed at production LLM applications, providing tools for monitoring, version control, and testing datasets.\\n- Llama2: An open-source large language model from Meta that can be fine-tuned and deployed.\\n- Openlayer: A platform focused on ML model testing, monitoring, and improvement.\\n- Backengine: A platform that allows creating and deploying backend APIs using natural language descriptions.\\n- QueryVary: A platform for systematically designing and refining prompts for LLMs.\\n\\nConclusion:\\nIn conclusion, LangSmith is a developer platform designed to simplify the development and management of Language Model applications (LLMs). It provides developers with tools for tracing, testing, evaluating, and monitoring LLM applications, along with comprehensive visibility and real-time insights. LangSmith aims to empower developers and handle the complexity of LLM applications effectively. By integrating seamlessly with LangChain, it offers enhanced capabilities for building applications with large language models. While LangSmith is a prominent platform, developers may also consider other alternatives such as LangChain, GradientJ, LLMOps.Space, Vellum, and more, depending on their specific requirements.']" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" } ], "source": [