{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from langchain.llms import OpenAI\n", "from langchain.chains import LLMRequestsChain, LLMChain" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import PromptTemplate\n", "\n", "template = \"\"\"Between >>> and <<< are the raw search result text from google.\n", "Extract the answer to the question '{query}' or say \"not found\" if the information is not contained.\n", "Use the format\n", "Extracted:\n", ">>> {requests_result} <<<\n", "Extracted:\"\"\"\n", "\n", "PROMPT = PromptTemplate(\n", " input_variables=[\"query\", \"requests_result\"],\n", " template=template,\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "chain = LLMRequestsChain(llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=PROMPT))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "question = \"今天是日期是?\"\n", "inputs = {\n", " \"query\": question,\n", " \"url\": \"https://www.google.com/search?q=\" + question.replace(\" \", \"+\")\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'query': '今天是日期是?',\n", " 'url': 'https://www.google.com/search?q=今天是日期是?',\n", " 'output': '2023年5月6日星期六'}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chain(inputs)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'query': '执行decode后,找出所有中文',\n", " 'url': 'https://i.zuoyebang.cc/odsv2/#/admin/weekly-stats',\n", " 'output': ' not found'}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "question = \"执行decode后,找出所有中文\"\n", "inputs = {\n", " \"query\": question,\n", " \"url\": \"https://i.zuoyebang.cc/odsv2/#/admin/weekly-stats\"\n", "}\n", "chain(inputs)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ç\\x9b®æ\\xa0\\x87管ç\\x90\\x86å¹³å\\x8f°
'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.utilities import TextRequestsWrapper\n", "requests = TextRequestsWrapper()\n", "requests.get(\"https://i.zuoyebang.cc/odsv2/#/admin/weekly-stats\")" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }