This commit is contained in:
Nuno Campos
2023-08-18 12:30:24 +01:00
parent 14a62fbc3f
commit 7c867d8dac
8 changed files with 130 additions and 100 deletions
+21 -29
View File
@@ -45,7 +45,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"id": "4e11ecdb-2b74-4f1e-8b8b-91a0d0e7547c",
"metadata": {},
"outputs": [],
@@ -62,17 +62,17 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"id": "85df70b5-4d3c-47b1-b401-036f513965b8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Arrr, matey! What be art, ye be askin'? Well, art be a broad term, encompassin' a vast array o' creative expressions. Be it paintin's, sculptures, music, or even the written word, art be a means o' expressin' oneself and communicatin' emotions. It be a way fer humans to tap into their imagination and create somethin' beautiful or thought-provokin'. Art be subjective, each eye seein' it differently, but it be an important part o' our culture, history, and identity. So, me hearties, let yer creativity run wild, and let art be yer compass on this grand adventure called life!\""
"\"Arrr, me hearty! Art be a thing of beauty and expression, me hearties! It be a way for a scurvy dog to convey their innermost thoughts and feelings through visual, auditory, or kinesthetic means. It be a form of communication that ye can't be puttin' into words, but ye can feel it in yer bones. Art be a treasure that be created by the talents and imaginations of landlubbers. Whether it be a paintin', a sculpture, a song, or a dance, art be a way to celebrate the wonders of this here world and let yer soul run wild on the high seas of creativity.\""
]
},
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
@@ -91,7 +91,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 4,
"id": "5371da31-1fd1-46dd-afaa-6727cc6a3d57",
"metadata": {},
"outputs": [],
@@ -131,17 +131,17 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 5,
"id": "8e2f7fa4-eef5-440e-bf51-ee236dc421e6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='', additional_kwargs={'function_call': {'name': 'revise', 'arguments': '{\\n\"notes\": \"The draft is too short and lacks content. Please provide a detailed and informative draft for review.\"\\n}'}}, example=False)"
"AIMessage(content='', additional_kwargs={'function_call': {'name': 'revise', 'arguments': '{\\n \"notes\": \"The draft is too short and lacks content. Please provide more information or expand on your current topic.\"\\n}'}}, example=False)"
]
},
"execution_count": 8,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
@@ -160,7 +160,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 6,
"id": "e9dcbbc9-2bc2-4a15-9002-1acb2692f943",
"metadata": {},
"outputs": [],
@@ -178,17 +178,17 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 7,
"id": "9f200a4c-628b-495b-a9ec-c647f8dcdcf0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Revised draft:\\n\\nHello!'"
"'Revised Draft:\\n\\nHello there!'"
]
},
"execution_count": 10,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@@ -212,7 +212,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 8,
"id": "82f6c33f-1553-4d92-bad9-0b499dafcf6c",
"metadata": {},
"outputs": [],
@@ -224,7 +224,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 9,
"id": "af624684-c3b4-4283-aecb-d57d1b2316f9",
"metadata": {},
"outputs": [],
@@ -240,7 +240,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 10,
"id": "3deabd3d-2995-4565-a6b2-38e39171143f",
"metadata": {},
"outputs": [],
@@ -270,7 +270,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 11,
"id": "f3aaf437-95d6-4e1f-a756-15b5132ac9f7",
"metadata": {},
"outputs": [],
@@ -286,7 +286,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 12,
"id": "314b75ee-837d-419c-81a7-ea3dec97203b",
"metadata": {},
"outputs": [],
@@ -299,17 +299,17 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 13,
"id": "0371a5ac-7194-4cf9-9dd2-56cb3d1f46d6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'draft': 'Turtles, fascinating creatures of the sea, are known for their diverse diets. They are omnivorous, meaning they consume both plant matter and small animals. Some turtles prefer a herbivorous diet, feeding on aquatic plants, seaweed, and algae. Others have a more carnivorous appetite, enjoying insects, fish, and crustaceans. Additionally, there are turtles that fall in the middle, being omnivores, and enjoying a variety of foods. So, when it comes to what turtles eat, they can be described as versatile eaters, ready to consume whatever comes their way in their marine habitat!'}]"
"[{'draft': 'Turtles are a highly diverse group of animals when it comes to their dietary preferences. Their food choices vary depending on the species and their surroundings. Sea turtles, for example, primarily consume sea grass, seaweed, and jellyfish. On the other hand, land turtles, like the well-known Tortuga, prefer leafy greens such as lettuce and kale. Certain turtles even include protein in their diets, enjoying insects, small fish, or even carrion. It is crucial to understand what your turtle likes to eat in order to maintain their well-being and happiness. However, it is always advisable to consult a veterinarian with expertise in turtles before making any changes to their diet.'}]"
]
},
"execution_count": 18,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -317,14 +317,6 @@
"source": [
"web_researcher.invoke({\"question\": \"What food do turtles eat?\"})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee462beb-2ce1-4516-908d-33d660bab5d4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -343,7 +335,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.1"
"version": "3.11.4"
}
},
"nbformat": 4,
+33 -26
View File
@@ -1,39 +1,42 @@
from operator import itemgetter
from langchain.chat_models import ChatOpenAI, ChatAnthropic
from langchain.prompts import SystemMessagePromptTemplate, ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser
from langchain.runnables.openai_functions import OpenAIFunctionsRouter
import requests
from fastapi import FastAPI
from langchain.chat_models import ChatAnthropic, ChatOpenAI
from langchain.output_parsers.openai_functions import JsonKeyOutputFunctionsParser
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate
from langchain.runnables.openai_functions import OpenAIFunctionsRouter
from langchain.schema.output_parser import StrOutputParser
from permchain.connection_inmemory import InMemoryPubSubConnection
from permchain.pubsub import PubSub
from permchain.topic import Topic
from langchain.output_parsers.openai_functions import JsonKeyOutputFunctionsParser
template = """Write between 2 and 5 sub questions that serve as google search queries to search online that form an objective opinion from the following: {question}"""
functions = [
{
"name": "sub_questions",
"description": "List of sub questions",
"parameters": {
"type": "object",
"properties": {
"questions": {
"type": "array",
"description": "List of sub questions to ask.",
"items": {
"type": "string"
}
},
},
"name": "sub_questions",
"description": "List of sub questions",
"parameters": {
"type": "object",
"properties": {
"questions": {
"type": "array",
"description": "List of sub questions to ask.",
"items": {"type": "string"},
},
},
},
},
]
prompt = ChatPromptTemplate.from_template(template)
question_chain = prompt | ChatOpenAI(temperature=0).bind(functions=functions, function_call={"name":"sub_questions"}) | JsonKeyOutputFunctionsParser(key_name="questions")
question_chain = (
prompt
| ChatOpenAI(temperature=0).bind(
functions=functions, function_call={"name": "sub_questions"}
)
| JsonKeyOutputFunctionsParser(key_name="questions")
)
template = """You are tasked with writing a research report to answer the following question:
@@ -54,10 +57,14 @@ report_chain = prompt | ChatOpenAI() | StrOutputParser()
research_inbox = Topic("research")
writer_inbox = Topic("writer_inbox")
def web_researcher(questions):
response = requests.post("http://127.0.0.1:8081/batch", json={"questions": questions})
response = requests.post(
"http://127.0.0.1:8081/batch", json={"questions": questions}
)
return response.json()
subquestion_actor = (
# Listed in inputs
Topic.IN.subscribe()
@@ -69,15 +76,13 @@ research_actor = (
research_inbox.subscribe()
| {
"research": lambda x: web_researcher(x),
#"research": (lambda x: [web_researcher(i) for i in x]),
# "research": (lambda x: [web_researcher(i) for i in x]),
"question": Topic.IN.current() | itemgetter("question"),
}
| writer_inbox.publish()
)
write_actor = (
writer_inbox.subscribe()
| {"response": report_chain}
| Topic.OUT.publish()
writer_inbox.subscribe() | {"response": report_chain} | Topic.OUT.publish()
)
longer_researcher = PubSub(
@@ -86,6 +91,8 @@ longer_researcher = PubSub(
)
app = FastAPI()
@app.get("/report")
def read_item(question: str):
return longer_researcher.invoke({"question":question})
return longer_researcher.invoke({"question": question})
+22 -11
View File
@@ -1,21 +1,29 @@
from operator import itemgetter
from typing import List
from langchain.chat_models import ChatOpenAI, ChatAnthropic
from langchain.prompts import SystemMessagePromptTemplate, ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser
from langchain.runnables.openai_functions import OpenAIFunctionsRouter
from pydantic import BaseModel
import requests
from fastapi import FastAPI
from langchain.chat_models import ChatAnthropic, ChatOpenAI
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate
from langchain.runnables.openai_functions import OpenAIFunctionsRouter
from langchain.schema.output_parser import StrOutputParser
from pydantic import BaseModel
from permchain.connection_inmemory import InMemoryPubSubConnection
from permchain.pubsub import PubSub
from permchain.topic import Topic
prompt = ChatPromptTemplate.from_template("Answer the user's question given the search results\n\n<question>{question}</question><search_results>{search_results}</search_results>")
prompt = ChatPromptTemplate.from_template(
"Answer the user's question given the search results\n\n<question>{question}</question><search_results>{search_results}</search_results>"
)
summarizer_chain = prompt | ChatOpenAI(max_retries=0).with_fallbacks([ChatOpenAI(model="gpt-3.5-turbo-16k"), 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()
)
def retrieve_documents(query):
@@ -35,9 +43,7 @@ search_actor = (
)
summ_actor = (
summarizer_inbox.subscribe()
| {"answer":summarizer_chain }
| Topic.OUT.publish()
summarizer_inbox.subscribe() | {"answer": summarizer_chain} | Topic.OUT.publish()
)
web_researcher = PubSub(
@@ -46,12 +52,17 @@ web_researcher = PubSub(
)
app = FastAPI()
class Data(BaseModel):
questions: List[str]
@app.get("/invoke")
def read_item(question: str):
return web_researcher.invoke(question)
@app.post("/batch")
def batch(data: Data):
return web_researcher.batch(data.questions)
+3 -1
View File
@@ -1,18 +1,20 @@
# main.py
from duckduckgo_search import DDGS
from fastapi import FastAPI
from langchain.document_loaders import AsyncHtmlLoader
from langchain.document_transformers import Html2TextTransformer
from duckduckgo_search import DDGS
ddgs = DDGS()
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/query")
def read_item(query: str):
query = query.strip().strip('"')
+1 -2
View File
@@ -2,14 +2,13 @@ from operator import itemgetter
from langchain.chat_models.openai import ChatOpenAI
from langchain.prompts import SystemMessagePromptTemplate
from langchain.schema.output_parser import StrOutputParser
from langchain.runnables.openai_functions import OpenAIFunctionsRouter
from langchain.schema.output_parser import StrOutputParser
from permchain.connection_inmemory import InMemoryPubSubConnection
from permchain.pubsub import PubSub
from permchain.topic import Topic
drafter_prompt = (
SystemMessagePromptTemplate.from_template(
"You are an expert on turtles, who likes to write in pirate-speak. You have been tasked by your editor with drafting a 100-word article answering the following question."
+35 -29
View File
@@ -67,7 +67,7 @@
"outputs": [],
"source": [
"def retrieve_documents(query):\n",
" query=query.strip().strip('\"')\n",
" query = query.strip().strip('\"')\n",
" search_results = ddgs.text(query)\n",
" urls_to_look = []\n",
" for res in search_results:\n",
@@ -75,7 +75,7 @@
" urls_to_look.append(res[\"href\"])\n",
" if len(urls_to_look) >= 4:\n",
" break\n",
" \n",
"\n",
" # Relevant urls\n",
" # Load, split, and add new urls to vectorstore\n",
" if urls_to_look:\n",
@@ -96,6 +96,7 @@
"outputs": [],
"source": [
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()"
]
},
@@ -106,7 +107,7 @@
"metadata": {},
"outputs": [],
"source": [
"#docs = retrieve_documents(\"langchain\")"
"# docs = retrieve_documents(\"langchain\")"
]
},
{
@@ -125,7 +126,9 @@
"metadata": {},
"outputs": [],
"source": [
"prompt = ChatPromptTemplate.from_template(\"Answer the user's question given the search results\\n\\n<question>{question}</question><search_results>{search_results}</search_results>\")"
"prompt = ChatPromptTemplate.from_template(\n",
" \"Answer the user's question given the search results\\n\\n<question>{question}</question><search_results>{search_results}</search_results>\"\n",
")"
]
},
{
@@ -135,7 +138,13 @@
"metadata": {},
"outputs": [],
"source": [
"summarizer_chain = prompt | ChatOpenAI(max_retries=0).with_fallbacks([ChatOpenAI(model=\"gpt-3.5-turbo-16k\"), ChatAnthropic(model=\"claude-2\")]) | StrOutputParser()"
"summarizer_chain = (\n",
" prompt\n",
" | ChatOpenAI(max_retries=0).with_fallbacks(\n",
" [ChatOpenAI(model=\"gpt-3.5-turbo-16k\"), ChatAnthropic(model=\"claude-2\")]\n",
" )\n",
" | StrOutputParser()\n",
")"
]
},
{
@@ -153,7 +162,6 @@
"metadata": {},
"outputs": [],
"source": [
"\n",
"summarizer_inbox = Topic(\"summarizer\")"
]
},
@@ -182,9 +190,7 @@
"outputs": [],
"source": [
"summ_actor = (\n",
" summarizer_inbox.subscribe()\n",
" | {\"answer\": summarizer_chain}\n",
" | Topic.OUT.publish()\n",
" summarizer_inbox.subscribe() | {\"answer\": summarizer_chain} | Topic.OUT.publish()\n",
")"
]
},
@@ -295,24 +301,28 @@
"template = \"\"\"Write between 2 and 5 sub questions that serve as google search queries to search online that form an objective opinion from the following: {question}\"\"\"\n",
"functions = [\n",
" {\n",
" \"name\": \"sub_questions\",\n",
" \"description\": \"List of sub questions\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"questions\": {\n",
" \"type\": \"array\",\n",
" \"description\": \"List of sub questions to ask.\",\n",
" \"items\": {\n",
" \"type\": \"string\"\n",
" }\n",
" },\n",
" },\n",
" \"name\": \"sub_questions\",\n",
" \"description\": \"List of sub questions\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"questions\": {\n",
" \"type\": \"array\",\n",
" \"description\": \"List of sub questions to ask.\",\n",
" \"items\": {\"type\": \"string\"},\n",
" },\n",
" },\n",
" },\n",
" },\n",
"]\n",
"prompt = ChatPromptTemplate.from_template(template)\n",
"question_chain = prompt | ChatOpenAI(temperature=0).bind(functions=functions, function_call={\"name\":\"sub_questions\"}) | JsonKeyOutputFunctionsParser(key_name=\"questions\")"
"question_chain = (\n",
" prompt\n",
" | ChatOpenAI(temperature=0).bind(\n",
" functions=functions, function_call={\"name\": \"sub_questions\"}\n",
" )\n",
" | JsonKeyOutputFunctionsParser(key_name=\"questions\")\n",
")"
]
},
{
@@ -393,16 +403,12 @@
" research_inbox.subscribe()\n",
" | {\n",
" \"research\": lambda x: web_researcher.batch(x),\n",
" #\"research\": lambda x: [web_researcher.invoke({\"question\": i}) for i in x],\n",
" # \"research\": lambda x: [web_researcher.invoke({\"question\": i}) for i in x],\n",
" \"question\": Topic.IN.current() | itemgetter(\"question\"),\n",
" }\n",
" | writer_inbox.publish()\n",
")\n",
"write_actor = (\n",
" writer_inbox.subscribe()\n",
" | report_chain\n",
" | Topic.OUT.publish()\n",
")"
"write_actor = writer_inbox.subscribe() | report_chain | Topic.OUT.publish()"
]
},
{
Generated
+14 -1
View File
@@ -360,10 +360,12 @@ files = [
[package.dependencies]
click = ">=8.0.0"
ipython = {version = ">=7.8.0", optional = true, markers = "extra == \"jupyter\""}
mypy-extensions = ">=0.4.3"
packaging = ">=22.0"
pathspec = ">=0.9.0"
platformdirs = ">=2"
tokenize-rt = {version = ">=3.2.0", optional = true, markers = "extra == \"jupyter\""}
tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""}
typing-extensions = {version = ">=3.10.0.0", markers = "python_version < \"3.10\""}
@@ -3165,6 +3167,17 @@ webencodings = ">=0.4"
doc = ["sphinx", "sphinx_rtd_theme"]
test = ["flake8", "isort", "pytest"]
[[package]]
name = "tokenize-rt"
version = "5.2.0"
description = "A wrapper around the stdlib `tokenize` which roundtrips."
optional = false
python-versions = ">=3.8"
files = [
{file = "tokenize_rt-5.2.0-py2.py3-none-any.whl", hash = "sha256:b79d41a65cfec71285433511b50271b05da3584a1da144a0752e9c621a285289"},
{file = "tokenize_rt-5.2.0.tar.gz", hash = "sha256:9fe80f8a5c1edad2d3ede0f37481cc0cc1538a2f442c9c2f9e4feacd2792d054"},
]
[[package]]
name = "tomli"
version = "2.0.1"
@@ -3457,4 +3470,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p
[metadata]
lock-version = "2.0"
python-versions = ">=3.8.1,<4.0"
content-hash = "3dc1864f26be637b3e52cdfef9a2bbbb2b1d4feeb245b5a3e105044e04dec2c8"
content-hash = "efcc0b5e1b923cc732d3f214a48e9e5b362ceb415839307fa82ac91839ac5aa7"
+1 -1
View File
@@ -25,7 +25,7 @@ syrupy = "^4.0.2"
[tool.poetry.group.lint.dependencies]
ruff = "^0.0.249"
black = "^23.1.0"
black = {extras = ["jupyter"], version = "^23.7.0"}
[tool.poetry.group.typing.dependencies]
mypy = "^0.991"