mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-20 22:03:52 +00:00
cr
This commit is contained in:
commit
bfe50949f5
@ -33,7 +33,7 @@ These are, in increasing order of complexity:
|
||||
1. LLM and Prompts
|
||||
2. Chains
|
||||
3. Agents
|
||||
4. (Coming Soon) Memory
|
||||
4. Memory
|
||||
|
||||
Let's go through these categories and for each one identify key concepts (to clarify terminology) as well as the problems in this area LangChain helps solve.
|
||||
|
||||
|
@ -48,7 +48,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.agents import ZeroShotAgent, Tool\n",
|
||||
"from langchain import OpenAI, SerpAPIChain, LLMChain"
|
||||
"from langchain import OpenAI, SerpAPIWrapper, LLMChain"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -58,7 +58,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"search = SerpAPIChain()\n",
|
||||
"search = SerpAPIWrapper()\n",
|
||||
"tools = [\n",
|
||||
" Tool(\n",
|
||||
" name = \"Search\",\n",
|
||||
|
@ -26,7 +26,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain import LLMMathChain, OpenAI, SerpAPIChain, SQLDatabase, SQLDatabaseChain\n",
|
||||
"from langchain import LLMMathChain, OpenAI, SerpAPIWrapper, SQLDatabase, SQLDatabaseChain\n",
|
||||
"from langchain.agents import initialize_agent, Tool"
|
||||
]
|
||||
},
|
||||
@ -38,7 +38,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = OpenAI(temperature=0)\n",
|
||||
"search = SerpAPIChain()\n",
|
||||
"search = SerpAPIWrapper()\n",
|
||||
"llm_math_chain = LLMMathChain(llm=llm, verbose=True)\n",
|
||||
"db = SQLDatabase.from_uri(\"sqlite:///../../../notebooks/Chinook.db\")\n",
|
||||
"db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)\n",
|
||||
|
@ -20,13 +20,17 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new SelfAskWithSearchAgent chain...\u001b[0m\n",
|
||||
"What is the hometown of the reigning men's U.S. Open champion?\n",
|
||||
"Are follow up questions needed here:\u001b[32;1m\u001b[1;3m Yes.\n",
|
||||
"Follow up: Who is the reigning men's U.S. Open champion?\u001b[0m\n",
|
||||
"Intermediate answer: \u001b[36;1m\u001b[1;3mCarlos Alcaraz\u001b[0m\n",
|
||||
"\u001b[32;1m\u001b[1;3mFollow up: Where is Carlos Alcaraz from?\u001b[0m\n",
|
||||
"Intermediate answer: \u001b[36;1m\u001b[1;3mEl Palmar, Spain\u001b[0m\n",
|
||||
"\u001b[32;1m\u001b[1;3mSo the final answer is: El Palmar, Spain\u001b[0m"
|
||||
"\u001b[32;1m\u001b[1;3mSo the final answer is: El Palmar, Spain\u001b[0m\n",
|
||||
"\u001b[1m> Finished SelfAskWithSearchAgent chain.\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -41,11 +45,11 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain import OpenAI, SerpAPIChain\n",
|
||||
"from langchain import OpenAI, SerpAPIWrapper\n",
|
||||
"from langchain.agents import initialize_agent, Tool\n",
|
||||
"\n",
|
||||
"llm = OpenAI(temperature=0)\n",
|
||||
"search = SerpAPIChain()\n",
|
||||
"search = SerpAPIWrapper()\n",
|
||||
"tools = [\n",
|
||||
" Tool(\n",
|
||||
" name=\"Intermediate Answer\",\n",
|
||||
|
200
docs/examples/chains/combine documents.ipynb
Normal file
200
docs/examples/chains/combine documents.ipynb
Normal file
@ -0,0 +1,200 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "efc5be67",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Question-Answering with Sources\n",
|
||||
"\n",
|
||||
"This notebook goes over how to do question-answering with sources. It does this in a few different ways - first showing how you can use the `QAWithSourcesChain` to take in documents and use those, and next showing the `VectorDBQAWithSourcesChain`, which also does the lookup of the documents from a vector database. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "1c613960",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
|
||||
"from langchain.embeddings.cohere import CohereEmbeddings\n",
|
||||
"from langchain.text_splitter import CharacterTextSplitter\n",
|
||||
"from langchain.vectorstores.elastic_vector_search import ElasticVectorSearch\n",
|
||||
"from langchain.vectorstores.faiss import FAISS"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "17d1306e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with open('../state_of_the_union.txt') as f:\n",
|
||||
" state_of_the_union = f.read()\n",
|
||||
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
|
||||
"texts = text_splitter.split_text(state_of_the_union)\n",
|
||||
"\n",
|
||||
"embeddings = OpenAIEmbeddings()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "0e745d99",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"docsearch = FAISS.from_texts(texts, embeddings)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "f42d79dc",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Add in a fake source information\n",
|
||||
"for i, d in enumerate(docsearch.docstore._dict.values()):\n",
|
||||
" d.metadata = {'source': f\"{i}-pl\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "aa1c1b60",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### QAWithSourcesChain\n",
|
||||
"This shows how to use the `QAWithSourcesChain`, which takes in document objects and uses them directly."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "61bce191",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"query = \"What did the president say about Justice Breyer\"\n",
|
||||
"docs = docsearch.similarity_search(query)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "57ddf8c7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import QAWithSourcesChain\n",
|
||||
"from langchain.llms import OpenAI, Cohere\n",
|
||||
"from langchain.docstore.document import Document"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "f908a92a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"chain = QAWithSourcesChain.from_llm(OpenAI(temperature=0))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "a505ac89",
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'answer': ' The president thanked Justice Breyer for his service.',\n",
|
||||
" 'sources': '27-pl'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"chain({\"docs\": docs, \"question\": query}, return_only_outputs=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e6fc81de",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### VectorDBQAWithSourcesChain\n",
|
||||
"\n",
|
||||
"This shows how to use the `VectorDBQAWithSourcesChain`, which uses a vector database to look up relevant documents."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "8aa571ae",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import VectorDBQAWithSourcesChain"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "aa859d4c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"chain = VectorDBQAWithSourcesChain.from_llm(OpenAI(temperature=0), vectorstore=docsearch)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8ba36fa7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"chain({\"question\": \"What did the president say about Justice Breyer\"}, return_only_outputs=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "980fae3b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.8.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
@ -39,7 +39,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 2,
|
||||
"id": "99bbe19b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -49,7 +49,7 @@
|
||||
"\"\\n\\nThe President discusses the recent aggression by Russia, and the response by the United States and its allies. He announces new sanctions against Russia, and says that the free world is united in holding Putin accountable. The President also discusses the American Rescue Plan, the Bipartisan Infrastructure Law, and the Bipartisan Innovation Act. Finally, the President addresses the need for women's rights and equality for LGBTQ+ Americans.\""
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -63,7 +63,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b581501e",
|
||||
"id": "baa6e808",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
|
180
docs/examples/chains/pal.ipynb
Normal file
180
docs/examples/chains/pal.ipynb
Normal file
@ -0,0 +1,180 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "32e022a2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# PAL\n",
|
||||
"\n",
|
||||
"Implements Program-Aided Language Models, as in https://arxiv.org/pdf/2211.10435.pdf.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "1370e40f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import PALChain\n",
|
||||
"from langchain import OpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "beddcac7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)\n",
|
||||
"pal_chain = PALChain.from_math_prompt(llm, verbose=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "e2eab9d4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"question = \"Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "3ef64b27",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new chain...\u001b[0m\n",
|
||||
"\u001b[32;1m\u001b[1;3mdef solution():\n",
|
||||
" \"\"\"Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?\"\"\"\n",
|
||||
" cindy_pets = 4\n",
|
||||
" marcia_pets = cindy_pets + 2\n",
|
||||
" jan_pets = marcia_pets * 3\n",
|
||||
" total_pets = cindy_pets + marcia_pets + jan_pets\n",
|
||||
" result = total_pets\n",
|
||||
" return result\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'28'"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"pal_chain.run(question)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "e524f81f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)\n",
|
||||
"pal_chain = PALChain.from_colored_object_prompt(llm, verbose=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "03a237b8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"question = \"On the desk, you see two blue booklets, two purple booklets, and two yellow pairs of sunglasses. If I remove all the pairs of sunglasses from the desk, how many purple items remain on it?\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "a84a4352",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new chain...\u001b[0m\n",
|
||||
"\u001b[32;1m\u001b[1;3m# Put objects into a list to record ordering\n",
|
||||
"objects = []\n",
|
||||
"objects += [('booklet', 'blue')] * 2\n",
|
||||
"objects += [('booklet', 'purple')] * 2\n",
|
||||
"objects += [('sunglasses', 'yellow')] * 2\n",
|
||||
"\n",
|
||||
"# Remove all pairs of sunglasses\n",
|
||||
"objects = [object for object in objects if object[0] != 'sunglasses']\n",
|
||||
"\n",
|
||||
"# Count number of purple objects\n",
|
||||
"num_purple = len([object for object in objects if object[1] == 'purple'])\n",
|
||||
"answer = num_purple\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'2'"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"pal_chain.run(question)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4ab20fec",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.8.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
@ -29,7 +29,7 @@
|
||||
"source": [
|
||||
"from langchain.agents import ZeroShotAgent, Tool\n",
|
||||
"from langchain.chains.conversation.memory import ConversationBufferMemory\n",
|
||||
"from langchain import OpenAI, SerpAPIChain, LLMChain"
|
||||
"from langchain import OpenAI, SerpAPIWrapper, LLMChain"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -39,7 +39,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"search = SerpAPIChain()\n",
|
||||
"search = SerpAPIWrapper()\n",
|
||||
"tools = [\n",
|
||||
" Tool(\n",
|
||||
" name = \"Search\",\n",
|
||||
|
@ -135,14 +135,14 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain import SelfAskWithSearchChain, SerpAPIChain\n",
|
||||
"from langchain import SelfAskWithSearchChain, SerpAPIWrapper\n",
|
||||
"\n",
|
||||
"open_ai_llm = OpenAI(temperature=0)\n",
|
||||
"search = SerpAPIChain()\n",
|
||||
"search = SerpAPIWrapper()\n",
|
||||
"self_ask_with_search_openai = SelfAskWithSearchChain(llm=open_ai_llm, search_chain=search, verbose=True)\n",
|
||||
"\n",
|
||||
"cohere_llm = Cohere(temperature=0, model=\"command-xlarge-20221108\")\n",
|
||||
"search = SerpAPIChain()\n",
|
||||
"search = SerpAPIWrapper()\n",
|
||||
"self_ask_with_search_cohere = SelfAskWithSearchChain(llm=cohere_llm, search_chain=search, verbose=True)"
|
||||
]
|
||||
},
|
||||
|
39
docs/explanation/cool_demos.md
Normal file
39
docs/explanation/cool_demos.md
Normal file
@ -0,0 +1,39 @@
|
||||
# Cool Demos
|
||||
|
||||
Lots of people have built some pretty awesome stuff with LangChain.
|
||||
This is a collection of our favorites.
|
||||
If you see any other demos that you think we should highlight, be sure to let us know!
|
||||
|
||||
## Open Source
|
||||
|
||||
### [ThoughtSource](https://github.com/OpenBioLink/ThoughtSource)
|
||||
A central, open resource and community around data and tools related to chain-of-thought reasoning in large language models.
|
||||
|
||||
### [Notion Database Question-Answering Bot](https://github.com/hwchase17/notion-qa)
|
||||
Open source GitHub project shows how to use LangChain to create a
|
||||
chatbot that can answer questions about an arbitrary Notion database.
|
||||
|
||||
### [GPT Index](https://github.com/jerryjliu/gpt_index)
|
||||
GPT Index is a project consisting of a set of data structures that are created using GPT-3 and can be traversed using GPT-3 in order to answer queries.
|
||||
|
||||
### [Grover's Algorithm](https://github.com/JavaFXpert/llm-grovers-search-party)
|
||||
Leveraging Qiskit, OpenAI and LangChain to demonstrate Grover's algorithm
|
||||
|
||||
|
||||
## Not Open Source
|
||||
|
||||
### [Daimon](https://twitter.com/sjwhitmore/status/1580593217153531908?s=20&t=neQvtZZTlp623U3LZwz3bQ)
|
||||
A chat-based AI personal assistant with long-term memory about you.
|
||||
|
||||
### [Clerkie](https://twitter.com/krrish_dh/status/1581028925618106368?s=20&t=neQvtZZTlp623U3LZwz3bQ)
|
||||
Stack Tracing QA Bot to help debug complex stack tracing (especially the ones that go multi-function/file deep).
|
||||
|
||||
### [Sales Email Writer](https://twitter.com/Raza_Habib496/status/1596880140490838017?s=20&t=6MqEQYWfSqmJwsKahjCVOA)
|
||||
By Raza Habib, this demo utilizes LangChain + SerpAPI + HumanLoop to write sales emails.
|
||||
Give it a company name and a person, this application will use Google Search (via SerpAPI) to get
|
||||
more information on the company and the person, and then write them a sales message.
|
||||
|
||||
### [Question-Answering on a Web Browser](https://twitter.com/chillzaza_/status/1592961099384905730?s=20&t=EhU8jl0KyCPJ7vE9Rnz-cQ)
|
||||
By Zahid Khawaja, this demo utilizes question answering to answer questions about a given website.
|
||||
A followup added this for [Youtube videos](https://twitter.com/chillzaza_/status/1593739682013220865?s=20&t=EhU8jl0KyCPJ7vE9Rnz-cQ),
|
||||
and then another followup added it for [Wikipedia](https://twitter.com/chillzaza_/status/1594847151238037505?s=20&t=EhU8jl0KyCPJ7vE9Rnz-cQ).
|
@ -77,9 +77,9 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Load the tool configs that are needed.\n",
|
||||
"from langchain import LLMMathChain, SerpAPIChain\n",
|
||||
"from langchain import LLMMathChain, SerpAPIWrapper\n",
|
||||
"llm = OpenAI(temperature=0)\n",
|
||||
"search = SerpAPIChain()\n",
|
||||
"search = SerpAPIWrapper()\n",
|
||||
"llm_math_chain = LLMMathChain(llm=llm, verbose=True)\n",
|
||||
"tools = [\n",
|
||||
" Tool(\n",
|
||||
|
@ -158,6 +158,7 @@ see detailed information about the various classes, methods, and APIs.
|
||||
explanation/core_concepts.md
|
||||
explanation/agents.md
|
||||
explanation/glossary.md
|
||||
explanation/cool_demos.md
|
||||
Discord <https://discord.gg/6adMQxSpJS>
|
||||
|
||||
Higher level, conceptual explanations of the LangChain components.
|
||||
|
@ -1 +1 @@
|
||||
0.0.22
|
||||
0.0.26
|
||||
|
@ -10,10 +10,12 @@ from langchain.chains import (
|
||||
ConversationChain,
|
||||
LLMChain,
|
||||
LLMMathChain,
|
||||
PALChain,
|
||||
PythonChain,
|
||||
SerpAPIChain,
|
||||
QAWithSourcesChain,
|
||||
SQLDatabaseChain,
|
||||
VectorDBQA,
|
||||
VectorDBQAWithSourcesChain,
|
||||
)
|
||||
from langchain.docstore import InMemoryDocstore, Wikipedia
|
||||
from langchain.llms import Cohere, HuggingFaceHub, OpenAI
|
||||
@ -23,6 +25,7 @@ from langchain.prompts import (
|
||||
Prompt,
|
||||
PromptTemplate,
|
||||
)
|
||||
from langchain.serpapi import SerpAPIChain, SerpAPIWrapper
|
||||
from langchain.sql_database import SQLDatabase
|
||||
from langchain.vectorstores import FAISS, ElasticVectorSearch
|
||||
|
||||
@ -31,6 +34,7 @@ __all__ = [
|
||||
"LLMMathChain",
|
||||
"PythonChain",
|
||||
"SelfAskWithSearchChain",
|
||||
"SerpAPIWrapper",
|
||||
"SerpAPIChain",
|
||||
"Cohere",
|
||||
"OpenAI",
|
||||
@ -49,4 +53,7 @@ __all__ = [
|
||||
"ElasticVectorSearch",
|
||||
"InMemoryDocstore",
|
||||
"ConversationChain",
|
||||
"VectorDBQAWithSourcesChain",
|
||||
"QAWithSourcesChain",
|
||||
"PALChain",
|
||||
]
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Chain that takes in an input and produces an action and action input."""
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, ClassVar, Dict, List, NamedTuple, Optional, Tuple
|
||||
|
||||
@ -87,8 +89,11 @@ class Agent(Chain, BaseModel, ABC):
|
||||
"""Create a prompt for this class."""
|
||||
return cls.prompt
|
||||
|
||||
def _prepare_for_new_call(self) -> None:
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def from_llm_and_tools(cls, llm: LLM, tools: List[Tool], **kwargs: Any) -> "Agent":
|
||||
def from_llm_and_tools(cls, llm: LLM, tools: List[Tool], **kwargs: Any) -> Agent:
|
||||
"""Construct an agent from an LLM and tools."""
|
||||
cls._validate_tools(tools)
|
||||
llm_chain = LLMChain(llm=llm, prompt=cls.create_prompt(tools))
|
||||
@ -119,6 +124,8 @@ class Agent(Chain, BaseModel, ABC):
|
||||
def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
|
||||
"""Run text through and get agent response."""
|
||||
text = inputs[self.input_key]
|
||||
# Do any preparation necessary when receiving a new input.
|
||||
self._prepare_for_new_call()
|
||||
# Construct a mapping of tool name to tool for easy lookup
|
||||
name_to_tool_map = {tool.name: tool.func for tool in self.tools}
|
||||
# Construct the initial string to pass into the LLM. This is made up
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Attempt to implement MRKL systems as described in arxiv.org/pdf/2205.00445.pdf."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Callable, List, NamedTuple, Optional, Tuple
|
||||
|
||||
from langchain.agents.agent import Agent
|
||||
@ -114,7 +116,7 @@ class MRKLChain(ZeroShotAgent):
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def from_chains(cls, llm: LLM, chains: List[ChainConfig], **kwargs: Any) -> "Agent":
|
||||
def from_chains(cls, llm: LLM, chains: List[ChainConfig], **kwargs: Any) -> Agent:
|
||||
"""User friendly way to initialize the MRKL chain.
|
||||
|
||||
This is intended to be an easy way to get up and running with the
|
||||
@ -131,10 +133,10 @@ class MRKLChain(ZeroShotAgent):
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
from langchain import LLMMathChain, OpenAI, SerpAPIChain, MRKLChain
|
||||
from langchain import LLMMathChain, OpenAI, SerpAPIWrapper, MRKLChain
|
||||
from langchain.chains.mrkl.base import ChainConfig
|
||||
llm = OpenAI(temperature=0)
|
||||
search = SerpAPIChain()
|
||||
search = SerpAPIWrapper()
|
||||
llm_math_chain = LLMMathChain(llm=llm)
|
||||
chains = [
|
||||
ChainConfig(
|
||||
|
@ -31,6 +31,9 @@ class ReActDocstoreAgent(Agent, BaseModel):
|
||||
f"Tool names should be Lookup and Search, got {tool_names}"
|
||||
)
|
||||
|
||||
def _prepare_for_new_call(self) -> None:
|
||||
self.i = 1
|
||||
|
||||
def _fix_text(self, text: str) -> str:
|
||||
return text + f"\nAction {self.i}:"
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
"""Chain that does self ask with search."""
|
||||
from typing import Any, ClassVar, List, Tuple
|
||||
from typing import Any, ClassVar, List, Optional, Tuple
|
||||
|
||||
from langchain.agents.agent import Agent
|
||||
from langchain.agents.self_ask_with_search.prompt import PROMPT
|
||||
from langchain.agents.tools import Tool
|
||||
from langchain.chains.llm import LLMChain
|
||||
from langchain.chains.serpapi import SerpAPIChain
|
||||
from langchain.llms.base import LLM
|
||||
from langchain.prompts.base import BasePromptTemplate
|
||||
from langchain.serpapi import SerpAPIWrapper
|
||||
|
||||
|
||||
class SelfAskWithSearchAgent(Agent):
|
||||
@ -25,31 +25,26 @@ class SelfAskWithSearchAgent(Agent):
|
||||
f"Tool name should be Intermediate Answer, got {tool_names}"
|
||||
)
|
||||
|
||||
def _extract_tool_and_input(self, text: str) -> Tuple[str, str]:
|
||||
def _extract_tool_and_input(self, text: str) -> Optional[Tuple[str, str]]:
|
||||
followup = "Follow up:"
|
||||
if "\n" not in text:
|
||||
last_line = text
|
||||
else:
|
||||
last_line = text.split("\n")[-1]
|
||||
|
||||
if followup not in last_line:
|
||||
finish_string = "So the final answer is: "
|
||||
if finish_string not in last_line:
|
||||
raise ValueError("We should probably never get here")
|
||||
return "Final Answer", text[len(finish_string) :]
|
||||
return None
|
||||
return "Final Answer", last_line[len(finish_string) :]
|
||||
|
||||
if ":" not in last_line:
|
||||
after_colon = last_line
|
||||
else:
|
||||
after_colon = text.split(":")[-1]
|
||||
|
||||
if " " == after_colon[0]:
|
||||
after_colon = after_colon[1:]
|
||||
if "?" != after_colon[-1]:
|
||||
print("we probably should never get here..." + text)
|
||||
|
||||
return "Intermediate Answer", after_colon
|
||||
|
||||
def _fix_text(self, text: str) -> str:
|
||||
return f"{text}\nSo the final answer is:"
|
||||
|
||||
@property
|
||||
def observation_prefix(self) -> str:
|
||||
"""Prefix to append the observation with."""
|
||||
@ -72,12 +67,12 @@ class SelfAskWithSearchChain(SelfAskWithSearchAgent):
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
from langchain import SelfAskWithSearchChain, OpenAI, SerpAPIChain
|
||||
search_chain = SerpAPIChain()
|
||||
from langchain import SelfAskWithSearchChain, OpenAI, SerpAPIWrapper
|
||||
search_chain = SerpAPIWrapper()
|
||||
self_ask = SelfAskWithSearchChain(llm=OpenAI(), search_chain=search_chain)
|
||||
"""
|
||||
|
||||
def __init__(self, llm: LLM, search_chain: SerpAPIChain, **kwargs: Any):
|
||||
def __init__(self, llm: LLM, search_chain: SerpAPIWrapper, **kwargs: Any):
|
||||
"""Initialize with just an LLM and a search chain."""
|
||||
search_tool = Tool(name="Intermediate Answer", func=search_chain.run)
|
||||
llm_chain = LLMChain(llm=llm, prompt=PROMPT)
|
||||
|
@ -28,13 +28,13 @@ So the final answer is: Joseph Ball
|
||||
Question: Are both the directors of Jaws and Casino Royale from the same country?
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: Who is the director of Jaws?
|
||||
Intermediate Answer: The director of Jaws is Steven Spielberg.
|
||||
Intermediate answer: The director of Jaws is Steven Spielberg.
|
||||
Follow up: Where is Steven Spielberg from?
|
||||
Intermediate Answer: The United States.
|
||||
Intermediate answer: The United States.
|
||||
Follow up: Who is the director of Casino Royale?
|
||||
Intermediate Answer: The director of Casino Royale is Martin Campbell.
|
||||
Intermediate answer: The director of Casino Royale is Martin Campbell.
|
||||
Follow up: Where is Martin Campbell from?
|
||||
Intermediate Answer: New Zealand.
|
||||
Intermediate answer: New Zealand.
|
||||
So the final answer is: No
|
||||
|
||||
Question: {input}"""
|
||||
|
@ -2,9 +2,11 @@
|
||||
from langchain.chains.conversation.base import ConversationChain
|
||||
from langchain.chains.llm import LLMChain
|
||||
from langchain.chains.llm_math.base import LLMMathChain
|
||||
from langchain.chains.pal.base import PALChain
|
||||
from langchain.chains.python import PythonChain
|
||||
from langchain.chains.qa_with_sources.base import QAWithSourcesChain
|
||||
from langchain.chains.qa_with_sources.vector_db import VectorDBQAWithSourcesChain
|
||||
from langchain.chains.sequential import SequentialChain, SimpleSequentialChain
|
||||
from langchain.chains.serpapi import SerpAPIChain
|
||||
from langchain.chains.sql_database.base import SQLDatabaseChain
|
||||
from langchain.chains.vector_db_qa.base import VectorDBQA
|
||||
|
||||
@ -12,10 +14,12 @@ __all__ = [
|
||||
"LLMChain",
|
||||
"LLMMathChain",
|
||||
"PythonChain",
|
||||
"SerpAPIChain",
|
||||
"SQLDatabaseChain",
|
||||
"VectorDBQA",
|
||||
"SequentialChain",
|
||||
"SimpleSequentialChain",
|
||||
"ConversationChain",
|
||||
"QAWithSourcesChain",
|
||||
"VectorDBQAWithSourcesChain",
|
||||
"PALChain",
|
||||
]
|
||||
|
@ -81,10 +81,12 @@ class Chain(BaseModel, ABC):
|
||||
inputs = dict(inputs, **external_context)
|
||||
self._validate_inputs(inputs)
|
||||
if self.verbose:
|
||||
print("\n\n\033[1m> Entering new chain...\033[0m")
|
||||
print(
|
||||
f"\n\n\033[1m> Entering new {self.__class__.__name__} chain...\033[0m"
|
||||
)
|
||||
outputs = self._call(inputs)
|
||||
if self.verbose:
|
||||
print("\n\033[1m> Finished chain.\033[0m")
|
||||
print(f"\n\033[1m> Finished {self.__class__.__name__} chain.\033[0m")
|
||||
self._validate_outputs(outputs)
|
||||
if self.memory is not None:
|
||||
self.memory.save_context(inputs, outputs)
|
||||
|
94
langchain/chains/combine_documents.py
Normal file
94
langchain/chains/combine_documents.py
Normal file
@ -0,0 +1,94 @@
|
||||
"""Document combining chain."""
|
||||
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from pydantic import BaseModel, Extra, Field, root_validator
|
||||
|
||||
from langchain.chains.base import Chain
|
||||
from langchain.chains.llm import LLMChain
|
||||
from langchain.prompts.base import BasePromptTemplate
|
||||
from langchain.prompts.prompt import Prompt
|
||||
|
||||
|
||||
def _get_default_document_prompt() -> Prompt:
|
||||
return Prompt(input_variables=["page_content"], template="{page_content}")
|
||||
|
||||
|
||||
class CombineDocumentsChain(Chain, BaseModel):
|
||||
"""Combine documents."""
|
||||
|
||||
llm_chain: LLMChain
|
||||
"""LLM wrapper to use after formatting documents."""
|
||||
document_prompt: BasePromptTemplate = Field(
|
||||
default_factory=_get_default_document_prompt
|
||||
)
|
||||
"""Prompt to use to format each document."""
|
||||
document_variable_name: str
|
||||
"""The variable name in the llm_chain to put the documents in.
|
||||
If only one variable in the llm_chain, this need not be provided."""
|
||||
input_key: str = "input_documents" #: :meta private:
|
||||
output_key: str = "output_text" #: :meta private:
|
||||
|
||||
class Config:
|
||||
"""Configuration for this pydantic object."""
|
||||
|
||||
extra = Extra.forbid
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
@property
|
||||
def input_keys(self) -> List[str]:
|
||||
"""Expect input key.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return [self.input_key]
|
||||
|
||||
@property
|
||||
def output_keys(self) -> List[str]:
|
||||
"""Return output key.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return [self.output_key]
|
||||
|
||||
@root_validator(pre=True)
|
||||
def get_default_document_variable_name(cls, values: Dict) -> Dict:
|
||||
"""Get default document variable name, if not provided."""
|
||||
if "document_variable_name" not in values:
|
||||
llm_chain_variables = values["llm_chain"].prompt.input_variables
|
||||
if len(llm_chain_variables) == 1:
|
||||
values["document_variable_name"] = llm_chain_variables[0]
|
||||
else:
|
||||
raise ValueError(
|
||||
"document_variable_name must be provided if there are "
|
||||
"multiple llm_chain_variables"
|
||||
)
|
||||
else:
|
||||
llm_chain_variables = values["llm_chain"].prompt.input_variables
|
||||
if values["document_variable_name"] not in llm_chain_variables:
|
||||
raise ValueError(
|
||||
f"document_variable_name {values['document_variable_name']} was "
|
||||
f"not found in llm_chain input_variables: {llm_chain_variables}"
|
||||
)
|
||||
return values
|
||||
|
||||
def _call(self, inputs: Dict[str, Any]) -> Dict[str, str]:
|
||||
docs = inputs[self.input_key]
|
||||
# Other keys are assumed to be needed for LLM prediction
|
||||
other_keys = {k: v for k, v in inputs.items() if k != self.input_key}
|
||||
# Get relevant information from each document.
|
||||
doc_dicts = []
|
||||
for doc in docs:
|
||||
base_info = {"page_content": doc.page_content}
|
||||
base_info.update(doc.metadata)
|
||||
document_info = {
|
||||
k: base_info[k] for k in self.document_prompt.input_variables
|
||||
}
|
||||
doc_dicts.append(document_info)
|
||||
# Format each document according to the prompt
|
||||
doc_strings = [self.document_prompt.format(**doc) for doc in doc_dicts]
|
||||
# Join the documents together to put them in the prompt.
|
||||
other_keys[self.document_variable_name] = "\n".join(doc_strings)
|
||||
# Call predict on the LLM.
|
||||
output = self.llm_chain.predict(**other_keys)
|
||||
return {self.output_key: output}
|
@ -84,8 +84,8 @@ class ConversationSummaryMemory(Memory, BaseModel):
|
||||
prompt_input_key = _get_prompt_input_key(inputs, self.memory_variables)
|
||||
if len(outputs) != 1:
|
||||
raise ValueError(f"One output key expected, got {outputs.keys()}")
|
||||
human = "Human: " + inputs[prompt_input_key]
|
||||
ai = "AI: " + list(outputs.values())[0]
|
||||
human = f"Human: {inputs[prompt_input_key]}"
|
||||
ai = f"AI: {list(outputs.values())[0]}"
|
||||
new_lines = "\n".join([human, ai])
|
||||
chain = LLMChain(llm=self.llm, prompt=self.prompt)
|
||||
self.buffer = chain.predict(summary=self.buffer, new_lines=new_lines)
|
||||
|
@ -3,13 +3,16 @@
|
||||
Splits up a document, sends the smaller parts to the LLM with one prompt,
|
||||
then combines the results with another one.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Dict, List
|
||||
|
||||
from pydantic import BaseModel, Extra
|
||||
|
||||
from langchain.chains.base import Chain
|
||||
from langchain.chains.combine_documents import CombineDocumentsChain
|
||||
from langchain.chains.llm import LLMChain
|
||||
from langchain.docstore.document import Document
|
||||
from langchain.llms.base import LLM
|
||||
from langchain.prompts.base import BasePromptTemplate
|
||||
from langchain.text_splitter import TextSplitter
|
||||
@ -30,7 +33,7 @@ class MapReduceChain(Chain, BaseModel):
|
||||
@classmethod
|
||||
def from_params(
|
||||
cls, llm: LLM, prompt: BasePromptTemplate, text_splitter: TextSplitter
|
||||
) -> "MapReduceChain":
|
||||
) -> MapReduceChain:
|
||||
"""Construct a map-reduce chain that uses the chain for map and reduce."""
|
||||
llm_chain = LLMChain(llm=llm, prompt=prompt)
|
||||
return cls(map_llm=llm_chain, reduce_llm=llm_chain, text_splitter=text_splitter)
|
||||
@ -66,10 +69,9 @@ class MapReduceChain(Chain, BaseModel):
|
||||
input_list = [{self.map_llm.prompt.input_variables[0]: d} for d in docs]
|
||||
summary_results = self.map_llm.apply(input_list)
|
||||
summaries = [res[self.map_llm.output_key] for res in summary_results]
|
||||
|
||||
summary_docs = [Document(page_content=text) for text in summaries]
|
||||
# We then need to combine these individual parts into one.
|
||||
# This is the reduce part.
|
||||
summary_str = "\n".join(summaries)
|
||||
inputs = {self.reduce_llm.prompt.input_variables[0]: summary_str}
|
||||
output = self.reduce_llm.predict(**inputs)
|
||||
return {self.output_key: output}
|
||||
reduce_chain = CombineDocumentsChain(llm_chain=self.reduce_llm)
|
||||
outputs = reduce_chain({reduce_chain.input_key: summary_docs})
|
||||
return {self.output_key: outputs[self.output_key]}
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Implement an LLM driven browser."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Dict, List
|
||||
|
||||
from pydantic import BaseModel, Extra
|
||||
@ -36,7 +38,7 @@ class NatBotChain(Chain, BaseModel):
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
@classmethod
|
||||
def from_default(cls, objective: str) -> "NatBotChain":
|
||||
def from_default(cls, objective: str) -> NatBotChain:
|
||||
"""Load with default LLM."""
|
||||
llm = OpenAI(temperature=0.5, best_of=10, n=3, max_tokens=50)
|
||||
return cls(llm=llm, objective=objective)
|
||||
|
4
langchain/chains/pal/__init__.py
Normal file
4
langchain/chains/pal/__init__.py
Normal file
@ -0,0 +1,4 @@
|
||||
"""Implements Program-Aided Language Models.
|
||||
|
||||
As in https://arxiv.org/pdf/2211.10435.pdf.
|
||||
"""
|
81
langchain/chains/pal/base.py
Normal file
81
langchain/chains/pal/base.py
Normal file
@ -0,0 +1,81 @@
|
||||
"""Implements Program-Aided Language Models.
|
||||
|
||||
As in https://arxiv.org/pdf/2211.10435.pdf.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from pydantic import BaseModel, Extra
|
||||
|
||||
from langchain.chains.base import Chain
|
||||
from langchain.chains.llm import LLMChain
|
||||
from langchain.chains.pal.colored_object_prompt import COLORED_OBJECT_PROMPT
|
||||
from langchain.chains.pal.math_prompt import MATH_PROMPT
|
||||
from langchain.chains.python import PythonChain
|
||||
from langchain.input import print_text
|
||||
from langchain.llms.base import LLM
|
||||
from langchain.prompts.base import BasePromptTemplate
|
||||
|
||||
|
||||
class PALChain(Chain, BaseModel):
|
||||
"""Implements Program-Aided Language Models."""
|
||||
|
||||
llm: LLM
|
||||
prompt: BasePromptTemplate
|
||||
stop: str = "\n\n"
|
||||
get_answer_expr: str = "print(solution())"
|
||||
output_key: str = "result" #: :meta private:
|
||||
|
||||
class Config:
|
||||
"""Configuration for this pydantic object."""
|
||||
|
||||
extra = Extra.forbid
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
@property
|
||||
def input_keys(self) -> List[str]:
|
||||
"""Return the singular input key.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return self.prompt.input_variables
|
||||
|
||||
@property
|
||||
def output_keys(self) -> List[str]:
|
||||
"""Return the singular output key.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return [self.output_key]
|
||||
|
||||
def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
|
||||
llm_chain = LLMChain(llm=self.llm, prompt=self.prompt)
|
||||
code = llm_chain.predict(stop=[self.stop], **inputs)
|
||||
if self.verbose:
|
||||
print_text(code, color="green", end="\n")
|
||||
repl = PythonChain()
|
||||
res = repl.run(code + f"\n{self.get_answer_expr}")
|
||||
return {self.output_key: res.strip()}
|
||||
|
||||
@classmethod
|
||||
def from_math_prompt(cls, llm: LLM, **kwargs: Any) -> PALChain:
|
||||
"""Load PAL from math prompt."""
|
||||
return cls(
|
||||
llm=llm,
|
||||
prompt=MATH_PROMPT,
|
||||
stop="\n\n",
|
||||
get_answer_expr="print(solution())",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_colored_object_prompt(cls, llm: LLM, **kwargs: Any) -> PALChain:
|
||||
"""Load PAL from colored object prompt."""
|
||||
return cls(
|
||||
llm=llm,
|
||||
prompt=COLORED_OBJECT_PROMPT,
|
||||
stop="\n\n\n",
|
||||
get_answer_expr="print(answer)",
|
||||
**kwargs,
|
||||
)
|
77
langchain/chains/pal/colored_object_prompt.py
Normal file
77
langchain/chains/pal/colored_object_prompt.py
Normal file
@ -0,0 +1,77 @@
|
||||
# flake8: noqa
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
|
||||
template = (
|
||||
"""
|
||||
# Generate Python3 Code to solve problems
|
||||
# Q: On the nightstand, there is a red pencil, a purple mug, a burgundy keychain, a fuchsia teddy bear, a black plate, and a blue stress ball. What color is the stress ball?
|
||||
# Put objects into a dictionary for quick look up
|
||||
objects = dict()
|
||||
objects['pencil'] = 'red'
|
||||
objects['mug'] = 'purple'
|
||||
objects['keychain'] = 'burgundy'
|
||||
objects['teddy bear'] = 'fuchsia'
|
||||
objects['plate'] = 'black'
|
||||
objects['stress ball'] = 'blue'
|
||||
|
||||
# Look up the color of stress ball
|
||||
stress_ball_color = objects['stress ball']
|
||||
answer = stress_ball_color
|
||||
|
||||
|
||||
# Q: On the table, you see a bunch of objects arranged in a row: a purple paperclip, a pink stress ball, a brown keychain, a green scrunchiephone charger, a mauve fidget spinner, and a burgundy pen. What is the color of the object directly to the right of the stress ball?
|
||||
# Put objects into a list to record ordering
|
||||
objects = []
|
||||
objects += [('paperclip', 'purple')] * 1
|
||||
objects += [('stress ball', 'pink')] * 1
|
||||
objects += [('keychain', 'brown')] * 1
|
||||
objects += [('scrunchiephone charger', 'green')] * 1
|
||||
objects += [('fidget spinner', 'mauve')] * 1
|
||||
objects += [('pen', 'burgundy')] * 1
|
||||
|
||||
# Find the index of the stress ball
|
||||
stress_ball_idx = None
|
||||
for i, object in enumerate(objects):
|
||||
if object[0] == 'stress ball':
|
||||
stress_ball_idx = i
|
||||
break
|
||||
|
||||
# Find the directly right object
|
||||
direct_right = objects[i+1]
|
||||
|
||||
# Check the directly right object's color
|
||||
direct_right_color = direct_right[1]
|
||||
answer = direct_right_color
|
||||
|
||||
|
||||
# Q: On the nightstand, you see the following items arranged in a row: a teal plate, a burgundy keychain, a yellow scrunchiephone charger, an orange mug, a pink notebook, and a grey cup. How many non-orange items do you see to the left of the teal item?
|
||||
# Put objects into a list to record ordering
|
||||
objects = []
|
||||
objects += [('plate', 'teal')] * 1
|
||||
objects += [('keychain', 'burgundy')] * 1
|
||||
objects += [('scrunchiephone charger', 'yellow')] * 1
|
||||
objects += [('mug', 'orange')] * 1
|
||||
objects += [('notebook', 'pink')] * 1
|
||||
objects += [('cup', 'grey')] * 1
|
||||
|
||||
# Find the index of the teal item
|
||||
teal_idx = None
|
||||
for i, object in enumerate(objects):
|
||||
if object[1] == 'teal':
|
||||
teal_idx = i
|
||||
break
|
||||
|
||||
# Find non-orange items to the left of the teal item
|
||||
non_orange = [object for object in objects[:i] if object[1] != 'orange']
|
||||
|
||||
# Count number of non-orange objects
|
||||
num_non_orange = len(non_orange)
|
||||
answer = num_non_orange
|
||||
|
||||
|
||||
# Q: {question}
|
||||
""".strip()
|
||||
+ "\n"
|
||||
)
|
||||
|
||||
COLORED_OBJECT_PROMPT = PromptTemplate(input_variables=["question"], template=template)
|
157
langchain/chains/pal/math_prompt.py
Normal file
157
langchain/chains/pal/math_prompt.py
Normal file
@ -0,0 +1,157 @@
|
||||
# flake8: noqa
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
|
||||
template = (
|
||||
'''
|
||||
Q: Olivia has $23. She bought five bagels for $3 each. How much money does she have left?
|
||||
|
||||
# solution in Python:
|
||||
|
||||
|
||||
def solution():
|
||||
"""Olivia has $23. She bought five bagels for $3 each. How much money does she have left?"""
|
||||
money_initial = 23
|
||||
bagels = 5
|
||||
bagel_cost = 3
|
||||
money_spent = bagels * bagel_cost
|
||||
money_left = money_initial - money_spent
|
||||
result = money_left
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Q: Michael had 58 golf balls. On tuesday, he lost 23 golf balls. On wednesday, he lost 2 more. How many golf balls did he have at the end of wednesday?
|
||||
|
||||
# solution in Python:
|
||||
|
||||
|
||||
def solution():
|
||||
"""Michael had 58 golf balls. On tuesday, he lost 23 golf balls. On wednesday, he lost 2 more. How many golf balls did he have at the end of wednesday?"""
|
||||
golf_balls_initial = 58
|
||||
golf_balls_lost_tuesday = 23
|
||||
golf_balls_lost_wednesday = 2
|
||||
golf_balls_left = golf_balls_initial - golf_balls_lost_tuesday - golf_balls_lost_wednesday
|
||||
result = golf_balls_left
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Q: There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room?
|
||||
|
||||
# solution in Python:
|
||||
|
||||
|
||||
def solution():
|
||||
"""There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room?"""
|
||||
computers_initial = 9
|
||||
computers_per_day = 5
|
||||
num_days = 4 # 4 days between monday and thursday
|
||||
computers_added = computers_per_day * num_days
|
||||
computers_total = computers_initial + computers_added
|
||||
result = computers_total
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Q: Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now?
|
||||
|
||||
# solution in Python:
|
||||
|
||||
|
||||
def solution():
|
||||
"""Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now?"""
|
||||
toys_initial = 5
|
||||
mom_toys = 2
|
||||
dad_toys = 2
|
||||
total_received = mom_toys + dad_toys
|
||||
total_toys = toys_initial + total_received
|
||||
result = total_toys
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Q: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?
|
||||
|
||||
# solution in Python:
|
||||
|
||||
|
||||
def solution():
|
||||
"""Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?"""
|
||||
jason_lollipops_initial = 20
|
||||
jason_lollipops_after = 12
|
||||
denny_lollipops = jason_lollipops_initial - jason_lollipops_after
|
||||
result = denny_lollipops
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Q: Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?
|
||||
|
||||
# solution in Python:
|
||||
|
||||
|
||||
def solution():
|
||||
"""Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?"""
|
||||
leah_chocolates = 32
|
||||
sister_chocolates = 42
|
||||
total_chocolates = leah_chocolates + sister_chocolates
|
||||
chocolates_eaten = 35
|
||||
chocolates_left = total_chocolates - chocolates_eaten
|
||||
result = chocolates_left
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Q: If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot?
|
||||
|
||||
# solution in Python:
|
||||
|
||||
|
||||
def solution():
|
||||
"""If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot?"""
|
||||
cars_initial = 3
|
||||
cars_arrived = 2
|
||||
total_cars = cars_initial + cars_arrived
|
||||
result = total_cars
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Q: There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done, there will be 21 trees. How many trees did the grove workers plant today?
|
||||
|
||||
# solution in Python:
|
||||
|
||||
|
||||
def solution():
|
||||
"""There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done, there will be 21 trees. How many trees did the grove workers plant today?"""
|
||||
trees_initial = 15
|
||||
trees_after = 21
|
||||
trees_added = trees_after - trees_initial
|
||||
result = trees_added
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Q: {question}
|
||||
|
||||
# solution in Python:
|
||||
'''.strip()
|
||||
+ "\n\n\n"
|
||||
)
|
||||
MATH_PROMPT = PromptTemplate(input_variables=["question"], template=template)
|
1
langchain/chains/qa_with_sources/__init__.py
Normal file
1
langchain/chains/qa_with_sources/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
"""Question answering with sources over documents."""
|
145
langchain/chains/qa_with_sources/base.py
Normal file
145
langchain/chains/qa_with_sources/base.py
Normal file
@ -0,0 +1,145 @@
|
||||
"""Question answering with sources over documents."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from pydantic import BaseModel, Extra, root_validator
|
||||
|
||||
from langchain.chains.base import Chain
|
||||
from langchain.chains.combine_documents import CombineDocumentsChain
|
||||
from langchain.chains.llm import LLMChain
|
||||
from langchain.chains.qa_with_sources.prompt import (
|
||||
COMBINE_PROMPT,
|
||||
EXAMPLE_PROMPT,
|
||||
QUESTION_PROMPT,
|
||||
)
|
||||
from langchain.docstore.document import Document
|
||||
from langchain.llms.base import LLM
|
||||
from langchain.prompts.base import BasePromptTemplate
|
||||
|
||||
|
||||
class BaseQAWithSourcesChain(Chain, BaseModel, ABC):
|
||||
"""Question answering with sources over documents."""
|
||||
|
||||
llm_question_chain: LLMChain
|
||||
"""LLM wrapper to use for asking questions to each document."""
|
||||
combine_document_chain: CombineDocumentsChain
|
||||
"""Chain to use to combine documents."""
|
||||
doc_source_key: str = "source"
|
||||
"""Key in document.metadata to use as source information"""
|
||||
question_key: str = "question" #: :meta private:
|
||||
input_docs_key: str = "docs" #: :meta private:
|
||||
answer_key: str = "answer" #: :meta private:
|
||||
sources_answer_key: str = "sources" #: :meta private:
|
||||
|
||||
@classmethod
|
||||
def from_llm(
|
||||
cls,
|
||||
llm: LLM,
|
||||
combine_document_prompt: BasePromptTemplate = EXAMPLE_PROMPT,
|
||||
question_prompt: BasePromptTemplate = QUESTION_PROMPT,
|
||||
combine_prompt: BasePromptTemplate = COMBINE_PROMPT,
|
||||
**kwargs: Any,
|
||||
) -> BaseQAWithSourcesChain:
|
||||
"""Construct the chain from an LLM."""
|
||||
llm_question_chain = LLMChain(llm=llm, prompt=question_prompt)
|
||||
llm_combine_chain = LLMChain(llm=llm, prompt=combine_prompt)
|
||||
combine_document_chain = CombineDocumentsChain(
|
||||
llm_chain=llm_combine_chain,
|
||||
document_prompt=combine_document_prompt,
|
||||
document_variable_name="summaries",
|
||||
)
|
||||
return cls(
|
||||
llm_question_chain=llm_question_chain,
|
||||
combine_document_chain=combine_document_chain,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
class Config:
|
||||
"""Configuration for this pydantic object."""
|
||||
|
||||
extra = Extra.forbid
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
@property
|
||||
def input_keys(self) -> List[str]:
|
||||
"""Expect input key.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return [self.question_key]
|
||||
|
||||
@property
|
||||
def output_keys(self) -> List[str]:
|
||||
"""Return output key.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return [self.answer_key, self.sources_answer_key]
|
||||
|
||||
@root_validator(pre=True)
|
||||
def validate_question_chain(cls, values: Dict) -> Dict:
|
||||
"""Validate question chain."""
|
||||
llm_question_chain = values["llm_question_chain"]
|
||||
if len(llm_question_chain.input_keys) != 2:
|
||||
raise ValueError(
|
||||
f"The llm_question_chain should have two inputs: a content key "
|
||||
f"(the first one) and a question key (the second one). Got "
|
||||
f"{llm_question_chain.input_keys}."
|
||||
)
|
||||
return values
|
||||
|
||||
@root_validator()
|
||||
def validate_combine_chain_can_be_constructed(cls, values: Dict) -> Dict:
|
||||
"""Validate that the combine chain can be constructed."""
|
||||
# Try to construct the combine documents chains.
|
||||
|
||||
return values
|
||||
|
||||
@abstractmethod
|
||||
def _get_docs(self, inputs: Dict[str, Any]) -> List[Document]:
|
||||
"""Get docs to run questioning over."""
|
||||
|
||||
def _call(self, inputs: Dict[str, Any]) -> Dict[str, str]:
|
||||
docs = self._get_docs(inputs)
|
||||
query = inputs[self.question_key]
|
||||
content_key, query_key = self.llm_question_chain.input_keys
|
||||
results = self.llm_question_chain.apply(
|
||||
[{content_key: d.page_content, query_key: query} for d in docs]
|
||||
)
|
||||
question_result_key = self.llm_question_chain.output_key
|
||||
result_docs = [
|
||||
Document(page_content=r[question_result_key], metadata=docs[i].metadata)
|
||||
for i, r in enumerate(results)
|
||||
]
|
||||
answer_dict = self.combine_document_chain(
|
||||
{
|
||||
self.combine_document_chain.input_key: result_docs,
|
||||
self.question_key: query,
|
||||
}
|
||||
)
|
||||
answer = answer_dict[self.combine_document_chain.output_key]
|
||||
if "\nSOURCES: " in answer:
|
||||
answer, sources = answer.split("\nSOURCES: ")
|
||||
else:
|
||||
sources = ""
|
||||
return {self.answer_key: answer, self.sources_answer_key: sources}
|
||||
|
||||
|
||||
class QAWithSourcesChain(BaseQAWithSourcesChain, BaseModel):
|
||||
"""Question answering with sources over documents."""
|
||||
|
||||
input_docs_key: str = "docs" #: :meta private:
|
||||
|
||||
@property
|
||||
def input_keys(self) -> List[str]:
|
||||
"""Expect input key.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return [self.input_docs_key, self.question_key]
|
||||
|
||||
def _get_docs(self, inputs: Dict[str, Any]) -> List[Document]:
|
||||
return inputs[self.input_docs_key]
|
55
langchain/chains/qa_with_sources/prompt.py
Normal file
55
langchain/chains/qa_with_sources/prompt.py
Normal file
@ -0,0 +1,55 @@
|
||||
# flake8: noqa
|
||||
from langchain.prompts import PromptTemplate
|
||||
|
||||
question_prompt_template = """Use the following portion of a long document to see if any of the text is relevant to answer the question.
|
||||
Return any relevant text verbatim.
|
||||
{context}
|
||||
Question: {question}
|
||||
Relevant text, if any:"""
|
||||
QUESTION_PROMPT = PromptTemplate(
|
||||
template=question_prompt_template, input_variables=["context", "question"]
|
||||
)
|
||||
|
||||
combine_prompt_template = """Given the following extracted parts of a long document and a question, create a final answer with references ("SOURCES").
|
||||
If you don't know the answer, just say that you don't know. Don't try to make up an answer.
|
||||
ALWAYS return a "SOURCES" part in your answer.
|
||||
|
||||
QUESTION: Which state/country's law governs the interpretation of the contract?
|
||||
=========
|
||||
Content: This Agreement is governed by English law and the parties submit to the exclusive jurisdiction of the English courts in relation to any dispute (contractual or non-contractual) concerning this Agreement save that either party may apply to any court for an injunction or other relief to protect its Intellectual Property Rights.
|
||||
Source: 28-pl
|
||||
Content: No Waiver. Failure or delay in exercising any right or remedy under this Agreement shall not constitute a waiver of such (or any other) right or remedy.\n\n11.7 Severability. The invalidity, illegality or unenforceability of any term (or part of a term) of this Agreement shall not affect the continuation in force of the remainder of the term (if any) and this Agreement.\n\n11.8 No Agency. Except as expressly stated otherwise, nothing in this Agreement shall create an agency, partnership or joint venture of any kind between the parties.\n\n11.9 No Third-Party Beneficiaries.
|
||||
Source: 30-pl
|
||||
Content: (b) if Google believes, in good faith, that the Distributor has violated or caused Google to violate any Anti-Bribery Laws (as defined in Clause 8.5) or that such a violation is reasonably likely to occur,
|
||||
Source: 4-pl
|
||||
=========
|
||||
FINAL ANSWER: This Agreement is governed by English law.
|
||||
SOURCES: 28-pl
|
||||
|
||||
QUESTION: What did the president say about Michael Jackson?
|
||||
=========
|
||||
Content: Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world. \n\nGroups of citizens blocking tanks with their bodies. Everyone from students to retirees teachers turned soldiers defending their homeland.
|
||||
Source: 0-pl
|
||||
Content: And we won’t stop. \n\nWe have lost so much to COVID-19. Time with one another. And worst of all, so much loss of life. \n\nLet’s use this moment to reset. Let’s stop looking at COVID-19 as a partisan dividing line and see it for what it is: A God-awful disease. \n\nLet’s stop seeing each other as enemies, and start seeing each other for who we really are: Fellow Americans. \n\nWe can’t change how divided we’ve been. But we can change how we move forward—on COVID-19 and other issues we must face together. \n\nI recently visited the New York City Police Department days after the funerals of Officer Wilbert Mora and his partner, Officer Jason Rivera. \n\nThey were responding to a 9-1-1 call when a man shot and killed them with a stolen gun. \n\nOfficer Mora was 27 years old. \n\nOfficer Rivera was 22. \n\nBoth Dominican Americans who’d grown up on the same streets they later chose to patrol as police officers. \n\nI spoke with their families and told them that we are forever in debt for their sacrifice, and we will carry on their mission to restore the trust and safety every community deserves.
|
||||
Source: 24-pl
|
||||
Content: And a proud Ukrainian people, who have known 30 years of independence, have repeatedly shown that they will not tolerate anyone who tries to take their country backwards. \n\nTo all Americans, I will be honest with you, as I’ve always promised. A Russian dictator, invading a foreign country, has costs around the world. \n\nAnd I’m taking robust action to make sure the pain of our sanctions is targeted at Russia’s economy. And I will use every tool at our disposal to protect American businesses and consumers. \n\nTonight, I can announce that the United States has worked with 30 other countries to release 60 Million barrels of oil from reserves around the world. \n\nAmerica will lead that effort, releasing 30 Million barrels from our own Strategic Petroleum Reserve. And we stand ready to do more if necessary, unified with our allies. \n\nThese steps will help blunt gas prices here at home. And I know the news about what’s happening can seem alarming. \n\nBut I want you to know that we are going to be okay.
|
||||
Source: 5-pl
|
||||
Content: More support for patients and families. \n\nTo get there, I call on Congress to fund ARPA-H, the Advanced Research Projects Agency for Health. \n\nIt’s based on DARPA—the Defense Department project that led to the Internet, GPS, and so much more. \n\nARPA-H will have a singular purpose—to drive breakthroughs in cancer, Alzheimer’s, diabetes, and more. \n\nA unity agenda for the nation. \n\nWe can do this. \n\nMy fellow Americans—tonight , we have gathered in a sacred space—the citadel of our democracy. \n\nIn this Capitol, generation after generation, Americans have debated great questions amid great strife, and have done great things. \n\nWe have fought for freedom, expanded liberty, defeated totalitarianism and terror. \n\nAnd built the strongest, freest, and most prosperous nation the world has ever known. \n\nNow is the hour. \n\nOur moment of responsibility. \n\nOur test of resolve and conscience, of history itself. \n\nIt is in this moment that our character is formed. Our purpose is found. Our future is forged. \n\nWell I know this nation.
|
||||
Source: 34-pl
|
||||
=========
|
||||
FINAL ANSWER: The president did not mention Michael Jackson.
|
||||
SOURCES:
|
||||
|
||||
QUESTION: {question}
|
||||
=========
|
||||
{summaries}
|
||||
=========
|
||||
FINAL ANSWER:"""
|
||||
COMBINE_PROMPT = PromptTemplate(
|
||||
template=combine_prompt_template, input_variables=["summaries", "question"]
|
||||
)
|
||||
|
||||
EXAMPLE_PROMPT = PromptTemplate(
|
||||
template="Content: {page_content}\nSource: {source}",
|
||||
input_variables=["page_content", "source"],
|
||||
)
|
20
langchain/chains/qa_with_sources/vector_db.py
Normal file
20
langchain/chains/qa_with_sources/vector_db.py
Normal file
@ -0,0 +1,20 @@
|
||||
"""Question-answering with sources over a vector database."""
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from langchain.chains.qa_with_sources.base import BaseQAWithSourcesChain
|
||||
from langchain.docstore.document import Document
|
||||
from langchain.vectorstores.base import VectorStore
|
||||
|
||||
|
||||
class VectorDBQAWithSourcesChain(BaseQAWithSourcesChain, BaseModel):
|
||||
"""Question-answering with sources over a vector database."""
|
||||
|
||||
vectorstore: VectorStore
|
||||
"""Vector Database to connect to."""
|
||||
k: int = 4
|
||||
|
||||
def _get_docs(self, inputs: Dict[str, Any]) -> List[Document]:
|
||||
question = inputs[self.question_key]
|
||||
return self.vectorstore.similarity_search(question, k=self.k)
|
@ -54,7 +54,7 @@ class SQLDatabaseChain(Chain, BaseModel):
|
||||
def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
|
||||
llm_chain = LLMChain(llm=self.llm, prompt=PROMPT)
|
||||
chained_input = ChainedInput(
|
||||
inputs[self.input_key] + "\nSQLQuery:", verbose=self.verbose
|
||||
f"{inputs[self.input_key]} \nSQLQuery:", verbose=self.verbose
|
||||
)
|
||||
llm_inputs = {
|
||||
"input": chained_input.input,
|
||||
|
@ -1,6 +1,12 @@
|
||||
"""Wrappers around embedding modules."""
|
||||
from langchain.embeddings.cohere import CohereEmbeddings
|
||||
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
||||
from langchain.embeddings.huggingface_hub import HuggingFaceHubEmbeddings
|
||||
from langchain.embeddings.openai import OpenAIEmbeddings
|
||||
|
||||
__all__ = ["OpenAIEmbeddings", "HuggingFaceEmbeddings", "CohereEmbeddings"]
|
||||
__all__ = [
|
||||
"OpenAIEmbeddings",
|
||||
"HuggingFaceEmbeddings",
|
||||
"CohereEmbeddings",
|
||||
"HuggingFaceHubEmbeddings",
|
||||
]
|
||||
|
@ -5,6 +5,8 @@ from pydantic import BaseModel, Extra
|
||||
|
||||
from langchain.embeddings.base import Embeddings
|
||||
|
||||
DEFAULT_MODEL_NAME = "sentence-transformers/all-mpnet-base-v2"
|
||||
|
||||
|
||||
class HuggingFaceEmbeddings(BaseModel, Embeddings):
|
||||
"""Wrapper around sentence_transformers embedding models.
|
||||
@ -16,11 +18,11 @@ class HuggingFaceEmbeddings(BaseModel, Embeddings):
|
||||
|
||||
from langchain.embeddings import HuggingFaceEmbeddings
|
||||
model_name = "sentence-transformers/all-mpnet-base-v2"
|
||||
huggingface = HuggingFaceEmbeddings(model_name=model_name)
|
||||
hf = HuggingFaceEmbeddings(model_name=model_name)
|
||||
"""
|
||||
|
||||
client: Any #: :meta private:
|
||||
model_name: str = "sentence-transformers/all-mpnet-base-v2"
|
||||
model_name: str = DEFAULT_MODEL_NAME
|
||||
"""Model name to use."""
|
||||
|
||||
def __init__(self, **kwargs: Any):
|
||||
|
105
langchain/embeddings/huggingface_hub.py
Normal file
105
langchain/embeddings/huggingface_hub.py
Normal file
@ -0,0 +1,105 @@
|
||||
"""Wrapper around HuggingFace Hub embedding models."""
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from pydantic import BaseModel, Extra, root_validator
|
||||
|
||||
from langchain.embeddings.base import Embeddings
|
||||
from langchain.utils import get_from_dict_or_env
|
||||
|
||||
DEFAULT_REPO_ID = "sentence-transformers/all-mpnet-base-v2"
|
||||
VALID_TASKS = ("feature-extraction",)
|
||||
|
||||
|
||||
class HuggingFaceHubEmbeddings(BaseModel, Embeddings):
|
||||
"""Wrapper around HuggingFaceHub embedding models.
|
||||
|
||||
To use, you should have the ``huggingface_hub`` python package installed, and the
|
||||
environment variable ``HUGGINGFACEHUB_API_TOKEN`` set with your API token, or pass
|
||||
it as a named parameter to the constructor.
|
||||
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
from langchain.embeddings import HuggingFaceHubEmbeddings
|
||||
repo_id = "sentence-transformers/all-mpnet-base-v2"
|
||||
hf = HuggingFaceHubEmbeddings(
|
||||
repo_id=repo_id,
|
||||
task="feature-extraction",
|
||||
huggingfacehub_api_token="my-api-key",
|
||||
)
|
||||
"""
|
||||
|
||||
client: Any #: :meta private:
|
||||
repo_id: str = DEFAULT_REPO_ID
|
||||
"""Model name to use."""
|
||||
task: Optional[str] = "feature-extraction"
|
||||
"""Task to call the model with."""
|
||||
model_kwargs: Optional[dict] = None
|
||||
"""Key word arguments to pass to the model."""
|
||||
|
||||
huggingfacehub_api_token: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
"""Configuration for this pydantic object."""
|
||||
|
||||
extra = Extra.forbid
|
||||
|
||||
@root_validator()
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
"""Validate that api key and python package exists in environment."""
|
||||
huggingfacehub_api_token = get_from_dict_or_env(
|
||||
values, "huggingfacehub_api_token", "HUGGINGFACEHUB_API_TOKEN"
|
||||
)
|
||||
try:
|
||||
from huggingface_hub.inference_api import InferenceApi
|
||||
|
||||
repo_id = values["repo_id"]
|
||||
if not repo_id.startswith("sentence-transformers"):
|
||||
raise ValueError(
|
||||
"Currently only 'sentence-transformers' embedding models "
|
||||
f"are supported. Got invalid 'repo_id' {repo_id}."
|
||||
)
|
||||
client = InferenceApi(
|
||||
repo_id=repo_id,
|
||||
token=huggingfacehub_api_token,
|
||||
task=values.get("task"),
|
||||
)
|
||||
if client.task not in VALID_TASKS:
|
||||
raise ValueError(
|
||||
f"Got invalid task {client.task}, "
|
||||
f"currently only {VALID_TASKS} are supported"
|
||||
)
|
||||
values["client"] = client
|
||||
except ImportError:
|
||||
raise ValueError(
|
||||
"Could not import huggingface_hub python package. "
|
||||
"Please it install it with `pip install huggingface_hub`."
|
||||
)
|
||||
return values
|
||||
|
||||
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
||||
"""Call out to HuggingFaceHub's embedding endpoint for embedding search docs.
|
||||
|
||||
Args:
|
||||
texts: The list of texts to embed.
|
||||
|
||||
Returns:
|
||||
List of embeddings, one for each text.
|
||||
"""
|
||||
# replace newlines, which can negatively affect performance.
|
||||
texts = [text.replace("\n", " ") for text in texts]
|
||||
_model_kwargs = self.model_kwargs or {}
|
||||
responses = self.client(inputs=texts, params=_model_kwargs)
|
||||
return responses
|
||||
|
||||
def embed_query(self, text: str) -> List[float]:
|
||||
"""Call out to HuggingFaceHub's embedding endpoint for embedding query text.
|
||||
|
||||
Args:
|
||||
text: The text to embed.
|
||||
|
||||
Returns:
|
||||
Embeddings for the text.
|
||||
"""
|
||||
response = self.embed_documents([text])[0]
|
||||
return response
|
@ -51,7 +51,7 @@ class HuggingFaceHub(LLM, BaseModel):
|
||||
try:
|
||||
from huggingface_hub.inference_api import InferenceApi
|
||||
|
||||
repo_id = values.get("repo_id", DEFAULT_REPO_ID)
|
||||
repo_id = values["repo_id"]
|
||||
client = InferenceApi(
|
||||
repo_id=repo_id,
|
||||
token=huggingfacehub_api_token,
|
||||
|
@ -20,11 +20,11 @@ class OpenAI(LLM, BaseModel):
|
||||
.. code-block:: python
|
||||
|
||||
from langchain import OpenAI
|
||||
openai = OpenAI(model="text-davinci-002")
|
||||
openai = OpenAI(model="text-davinci-003")
|
||||
"""
|
||||
|
||||
client: Any #: :meta private:
|
||||
model_name: str = "text-davinci-002"
|
||||
model_name: str = "text-davinci-003"
|
||||
"""Model name to use."""
|
||||
temperature: float = 0.7
|
||||
"""What sampling temperature to use."""
|
||||
@ -82,7 +82,7 @@ class OpenAI(LLM, BaseModel):
|
||||
return values
|
||||
|
||||
@property
|
||||
def _default_params(self) -> Mapping[str, Any]:
|
||||
def _default_params(self) -> Dict[str, Any]:
|
||||
"""Get the default parameters for calling OpenAI API."""
|
||||
normal_params = {
|
||||
"temperature": self.temperature,
|
||||
@ -115,7 +115,10 @@ class OpenAI(LLM, BaseModel):
|
||||
|
||||
response = openai("Tell me a joke.")
|
||||
"""
|
||||
response = self.client.create(
|
||||
model=self.model_name, prompt=prompt, stop=stop, **self._default_params
|
||||
)
|
||||
params = self._default_params
|
||||
if stop is not None:
|
||||
if "stop" in params:
|
||||
raise ValueError("`stop` found in both the input and default params.")
|
||||
params["stop"] = stop
|
||||
response = self.client.create(model=self.model_name, prompt=prompt, **params)
|
||||
return response["choices"][0]["text"]
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Experiment with different models."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Optional, Sequence, Union
|
||||
|
||||
from langchain.agents.agent import Agent
|
||||
@ -49,7 +51,7 @@ class ModelLaboratory:
|
||||
@classmethod
|
||||
def from_llms(
|
||||
cls, llms: List[LLM], prompt: Optional[PromptTemplate] = None
|
||||
) -> "ModelLaboratory":
|
||||
) -> ModelLaboratory:
|
||||
"""Initialize with LLMs to experiment with and optional prompt.
|
||||
|
||||
Args:
|
||||
|
@ -1,7 +1,10 @@
|
||||
"""BasePrompt schema definition."""
|
||||
import json
|
||||
from abc import ABC, abstractmethod
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
import yaml
|
||||
from pydantic import BaseModel, Extra, root_validator
|
||||
|
||||
from langchain.formatting import formatter
|
||||
@ -85,3 +88,39 @@ class BasePromptTemplate(BaseModel, ABC):
|
||||
|
||||
prompt.format(variable1="foo")
|
||||
"""
|
||||
|
||||
def _prompt_dict(self) -> Dict:
|
||||
"""Return a dictionary of the prompt."""
|
||||
return self.dict()
|
||||
|
||||
def save(self, file_path: Union[Path, str]) -> None:
|
||||
"""Save the prompt.
|
||||
|
||||
Args:
|
||||
file_path: Path to directory to save prompt to.
|
||||
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
prompt.save(file_path="path/prompt.yaml")
|
||||
"""
|
||||
# Convert file to Path object.
|
||||
if isinstance(file_path, str):
|
||||
save_path = Path(file_path)
|
||||
else:
|
||||
save_path = file_path
|
||||
|
||||
directory_path = save_path.parent
|
||||
directory_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Fetch dictionary to save
|
||||
prompt_dict = self._prompt_dict()
|
||||
|
||||
if save_path.suffix == ".json":
|
||||
with open(file_path, "w") as f:
|
||||
json.dump(prompt_dict, f, indent=4)
|
||||
elif save_path.suffix == ".yaml":
|
||||
with open(file_path, "w") as f:
|
||||
yaml.dump(prompt_dict, f, default_flow_style=False)
|
||||
else:
|
||||
raise ValueError(f"{save_path} must be json or yaml")
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Example selector that selects examples based on SemanticSimilarity."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from pydantic import BaseModel, Extra
|
||||
@ -8,6 +10,11 @@ from langchain.prompts.example_selector.base import BaseExampleSelector
|
||||
from langchain.vectorstores.base import VectorStore
|
||||
|
||||
|
||||
def sorted_values(values: Dict[str, str]) -> List[Any]:
|
||||
"""Return a list of values in dict sorted by key."""
|
||||
return [values[val] for val in sorted(values)]
|
||||
|
||||
|
||||
class SemanticSimilarityExampleSelector(BaseExampleSelector, BaseModel):
|
||||
"""Example selector that selects examples based on SemanticSimilarity."""
|
||||
|
||||
@ -26,13 +33,13 @@ class SemanticSimilarityExampleSelector(BaseExampleSelector, BaseModel):
|
||||
|
||||
def add_example(self, example: Dict[str, str]) -> None:
|
||||
"""Add new example to vectorstore."""
|
||||
string_example = " ".join(example.values())
|
||||
string_example = " ".join(sorted_values(example))
|
||||
self.vectorstore.add_texts([string_example], metadatas=[example])
|
||||
|
||||
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
|
||||
"""Select which examples to use based on semantic similarity."""
|
||||
# Get the docs with the highest similarity.
|
||||
query = " ".join(input_variables.values())
|
||||
query = " ".join(sorted_values(input_variables))
|
||||
example_docs = self.vectorstore.similarity_search(query, k=self.k)
|
||||
# Get the examples from the metadata.
|
||||
# This assumes that examples are stored in metadata.
|
||||
@ -50,7 +57,7 @@ class SemanticSimilarityExampleSelector(BaseExampleSelector, BaseModel):
|
||||
vectorstore_cls: VectorStore,
|
||||
k: int = 4,
|
||||
**vectorstore_cls_kwargs: Any,
|
||||
) -> "SemanticSimilarityExampleSelector":
|
||||
) -> SemanticSimilarityExampleSelector:
|
||||
"""Create k-shot example selector using example list and embeddings.
|
||||
|
||||
Reshuffles examples dynamically based on query similarity.
|
||||
@ -73,7 +80,7 @@ class SemanticSimilarityExampleSelector(BaseExampleSelector, BaseModel):
|
||||
Returns:
|
||||
The ExampleSelector instantiated, backed by a vector store.
|
||||
"""
|
||||
string_examples = [" ".join(eg.values()) for eg in examples]
|
||||
string_examples = [" ".join(sorted_values(eg)) for eg in examples]
|
||||
vectorstore = vectorstore_cls.from_texts(
|
||||
string_examples, embeddings, metadatas=examples, **vectorstore_cls_kwargs
|
||||
)
|
||||
|
@ -108,3 +108,12 @@ class FewShotPromptTemplate(BasePromptTemplate, BaseModel):
|
||||
template = self.example_separator.join([piece for piece in pieces if piece])
|
||||
# Format the template with the input variables.
|
||||
return DEFAULT_FORMATTER_MAPPING[self.template_format](template, **kwargs)
|
||||
|
||||
def _prompt_dict(self) -> Dict:
|
||||
"""Return a dictionary of the prompt."""
|
||||
if self.example_selector:
|
||||
raise ValueError("Saving an example selector is not currently supported")
|
||||
|
||||
prompt_dict = self.dict()
|
||||
prompt_dict["_type"] = "few_shot"
|
||||
return prompt_dict
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Prompt schema definition."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from pydantic import BaseModel, Extra, root_validator
|
||||
@ -67,7 +69,7 @@ class PromptTemplate(BasePromptTemplate, BaseModel):
|
||||
input_variables: List[str],
|
||||
example_separator: str = "\n\n",
|
||||
prefix: str = "",
|
||||
) -> "PromptTemplate":
|
||||
) -> PromptTemplate:
|
||||
"""Take examples in list format with prefix and suffix to create a prompt.
|
||||
|
||||
Intended be used as a way to dynamically create a prompt from examples.
|
||||
@ -92,7 +94,7 @@ class PromptTemplate(BasePromptTemplate, BaseModel):
|
||||
@classmethod
|
||||
def from_file(
|
||||
cls, template_file: str, input_variables: List[str]
|
||||
) -> "PromptTemplate":
|
||||
) -> PromptTemplate:
|
||||
"""Load a prompt from a file.
|
||||
|
||||
Args:
|
||||
|
@ -4,11 +4,10 @@ Heavily borrowed from https://github.com/ofirpress/self-ask
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
from typing import Any, Dict, List, Optional
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from pydantic import BaseModel, Extra, root_validator
|
||||
|
||||
from langchain.chains.base import Chain
|
||||
from langchain.utils import get_from_dict_or_env
|
||||
|
||||
|
||||
@ -26,8 +25,8 @@ class HiddenPrints:
|
||||
sys.stdout = self._original_stdout
|
||||
|
||||
|
||||
class SerpAPIChain(Chain, BaseModel):
|
||||
"""Chain that calls SerpAPI.
|
||||
class SerpAPIWrapper(BaseModel):
|
||||
"""Wrapper around SerpAPI.
|
||||
|
||||
To use, you should have the ``google-search-results`` python package installed,
|
||||
and the environment variable ``SERPAPI_API_KEY`` set with your API key, or pass
|
||||
@ -36,13 +35,11 @@ class SerpAPIChain(Chain, BaseModel):
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
from langchain import SerpAPIChain
|
||||
serpapi = SerpAPIChain()
|
||||
from langchain import SerpAPIWrapper
|
||||
serpapi = SerpAPIWrapper()
|
||||
"""
|
||||
|
||||
search_engine: Any #: :meta private:
|
||||
input_key: str = "search_query" #: :meta private:
|
||||
output_key: str = "search_result" #: :meta private:
|
||||
|
||||
serpapi_api_key: Optional[str] = None
|
||||
|
||||
@ -51,22 +48,6 @@ class SerpAPIChain(Chain, BaseModel):
|
||||
|
||||
extra = Extra.forbid
|
||||
|
||||
@property
|
||||
def input_keys(self) -> List[str]:
|
||||
"""Return the singular input key.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return [self.input_key]
|
||||
|
||||
@property
|
||||
def output_keys(self) -> List[str]:
|
||||
"""Return the singular output key.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return [self.output_key]
|
||||
|
||||
@root_validator()
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
"""Validate that api key and python package exists in environment."""
|
||||
@ -85,11 +66,12 @@ class SerpAPIChain(Chain, BaseModel):
|
||||
)
|
||||
return values
|
||||
|
||||
def _call(self, inputs: Dict[str, Any]) -> Dict[str, str]:
|
||||
def run(self, query: str) -> str:
|
||||
"""Run query through SerpAPI and parse result."""
|
||||
params = {
|
||||
"api_key": self.serpapi_api_key,
|
||||
"engine": "google",
|
||||
"q": inputs[self.input_key],
|
||||
"q": query,
|
||||
"google_domain": "google.com",
|
||||
"gl": "us",
|
||||
"hl": "en",
|
||||
@ -112,4 +94,9 @@ class SerpAPIChain(Chain, BaseModel):
|
||||
toret = res["organic_results"][0]["snippet"]
|
||||
else:
|
||||
toret = "No good search result found"
|
||||
return {self.output_key: toret}
|
||||
return toret
|
||||
|
||||
|
||||
# For backwards compatability
|
||||
|
||||
SerpAPIChain = SerpAPIWrapper
|
@ -1,4 +1,6 @@
|
||||
"""SQLAlchemy wrapper around a database."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Iterable, List, Optional
|
||||
|
||||
from sqlalchemy import create_engine, inspect
|
||||
@ -37,7 +39,7 @@ class SQLDatabase:
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_uri(cls, database_uri: str, **kwargs: Any) -> "SQLDatabase":
|
||||
def from_uri(cls, database_uri: str, **kwargs: Any) -> SQLDatabase:
|
||||
"""Construct a SQLAlchemy engine from URI."""
|
||||
return cls(create_engine(database_uri), **kwargs)
|
||||
|
||||
@ -66,6 +68,14 @@ class SQLDatabase:
|
||||
return "\n".join(tables)
|
||||
|
||||
def run(self, command: str) -> str:
|
||||
"""Execute a SQL command and return a string of the results."""
|
||||
result = self._engine.execute(command).fetchall()
|
||||
"""Execute a SQL command and return a string representing the results.
|
||||
|
||||
If the statement returns rows, a string of the results is returned.
|
||||
If the statement returns no rows, an empty string is returned.
|
||||
"""
|
||||
with self._engine.connect() as connection:
|
||||
cursor = connection.exec_driver_sql(command)
|
||||
if cursor.returns_rows:
|
||||
result = cursor.fetchall()
|
||||
return str(result)
|
||||
return ""
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Functionality for splitting text."""
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, Callable, Iterable, List
|
||||
|
||||
@ -46,9 +48,7 @@ class TextSplitter(ABC):
|
||||
return docs
|
||||
|
||||
@classmethod
|
||||
def from_huggingface_tokenizer(
|
||||
cls, tokenizer: Any, **kwargs: Any
|
||||
) -> "TextSplitter":
|
||||
def from_huggingface_tokenizer(cls, tokenizer: Any, **kwargs: Any) -> TextSplitter:
|
||||
"""Text splitter than uses HuggingFace tokenizer to count length."""
|
||||
try:
|
||||
from transformers import PreTrainedTokenizerBase
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Interface for vector stores."""
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, Iterable, List, Optional
|
||||
|
||||
@ -26,6 +28,6 @@ class VectorStore(ABC):
|
||||
texts: List[str],
|
||||
embedding: Embeddings,
|
||||
metadatas: Optional[List[dict]] = None,
|
||||
**kwargs: Any
|
||||
) -> "VectorStore":
|
||||
**kwargs: Any,
|
||||
) -> VectorStore:
|
||||
"""Return VectorStore initialized from texts and embeddings."""
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Wrapper around Elasticsearch vector database."""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from typing import Any, Callable, Dict, Iterable, List, Optional
|
||||
|
||||
@ -117,7 +119,7 @@ class ElasticVectorSearch(VectorStore):
|
||||
embedding: Embeddings,
|
||||
metadatas: Optional[List[dict]] = None,
|
||||
**kwargs: Any,
|
||||
) -> "ElasticVectorSearch":
|
||||
) -> ElasticVectorSearch:
|
||||
"""Construct ElasticVectorSearch wrapper from raw documents.
|
||||
|
||||
This is a user-friendly interface that:
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Wrapper around FAISS vector database."""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from typing import Any, Callable, Dict, Iterable, List, Optional
|
||||
|
||||
@ -96,7 +98,7 @@ class FAISS(VectorStore):
|
||||
embedding: Embeddings,
|
||||
metadatas: Optional[List[dict]] = None,
|
||||
**kwargs: Any,
|
||||
) -> "FAISS":
|
||||
) -> FAISS:
|
||||
"""Construct FAISS wrapper from raw documents.
|
||||
|
||||
This is a user friendly interface that:
|
||||
|
2
setup.py
2
setup.py
@ -6,7 +6,7 @@ from setuptools import find_packages, setup
|
||||
with open(Path(__file__).absolute().parents[0] / "langchain" / "VERSION") as _f:
|
||||
__version__ = _f.read().strip()
|
||||
|
||||
with open("README.md", "r") as f:
|
||||
with open("README.md", "r", encoding="utf-8") as f:
|
||||
long_description = f.read()
|
||||
|
||||
LLM_DEPENDENCIES = ["cohere", "openai", "nlpcloud", "huggingface_hub"]
|
||||
|
31
tests/integration_tests/chains/test_pal.py
Normal file
31
tests/integration_tests/chains/test_pal.py
Normal file
@ -0,0 +1,31 @@
|
||||
"""Test PAL chain."""
|
||||
|
||||
from langchain import OpenAI
|
||||
from langchain.chains.pal.base import PALChain
|
||||
|
||||
|
||||
def test_math_prompt() -> None:
|
||||
"""Test math prompt."""
|
||||
llm = OpenAI(model_name="code-davinci-002", temperature=0, max_tokens=512)
|
||||
pal_chain = PALChain.from_math_prompt(llm)
|
||||
question = (
|
||||
"Jan has three times the number of pets as Marcia. "
|
||||
"Marcia has two more pets than Cindy. "
|
||||
"If Cindy has four pets, how many total pets do the three have?"
|
||||
)
|
||||
output = pal_chain.run(question)
|
||||
assert output == "28"
|
||||
|
||||
|
||||
def test_colored_object_prompt() -> None:
|
||||
"""Test colored object prompt."""
|
||||
llm = OpenAI(model_name="code-davinci-002", temperature=0, max_tokens=512)
|
||||
pal_chain = PALChain.from_colored_object_prompt(llm)
|
||||
question = (
|
||||
"On the desk, you see two blue booklets, "
|
||||
"two purple booklets, and two yellow pairs of sunglasses. "
|
||||
"If I remove all the pairs of sunglasses from the desk, "
|
||||
"how many purple items remain on it?"
|
||||
)
|
||||
output = pal_chain.run(question)
|
||||
assert output == "2"
|
@ -1,7 +1,7 @@
|
||||
"""Integration test for self ask with search."""
|
||||
from langchain.agents.self_ask_with_search.base import SelfAskWithSearchChain
|
||||
from langchain.chains.serpapi import SerpAPIChain
|
||||
from langchain.llms.openai import OpenAI
|
||||
from langchain.serpapi import SerpAPIWrapper
|
||||
|
||||
|
||||
def test_self_ask_with_search() -> None:
|
||||
@ -9,7 +9,7 @@ def test_self_ask_with_search() -> None:
|
||||
question = "What is the hometown of the reigning men's U.S. Open champion?"
|
||||
chain = SelfAskWithSearchChain(
|
||||
llm=OpenAI(temperature=0),
|
||||
search_chain=SerpAPIChain(),
|
||||
search_chain=SerpAPIWrapper(),
|
||||
input_key="q",
|
||||
output_key="a",
|
||||
)
|
||||
|
@ -28,3 +28,20 @@ def test_sql_database_run() -> None:
|
||||
output = db_chain.run("What company does Harrison work at?")
|
||||
expected_output = " Harrison works at Foo."
|
||||
assert output == expected_output
|
||||
|
||||
|
||||
def test_sql_database_run_update() -> None:
|
||||
"""Test that update commands run successfully and returned in correct format."""
|
||||
engine = create_engine("sqlite:///:memory:")
|
||||
metadata_obj.create_all(engine)
|
||||
stmt = insert(user).values(user_id=13, user_name="Harrison", user_company="Foo")
|
||||
with engine.connect() as conn:
|
||||
conn.execute(stmt)
|
||||
db = SQLDatabase(engine)
|
||||
db_chain = SQLDatabaseChain(llm=OpenAI(temperature=0), database=db)
|
||||
output = db_chain.run("Update Harrison's workplace to Bar")
|
||||
expected_output = " Harrison's workplace has been updated to Bar."
|
||||
assert output == expected_output
|
||||
output = db_chain.run("What company does Harrison work at?")
|
||||
expected_output = " Harrison works at Bar."
|
||||
assert output == expected_output
|
||||
|
28
tests/integration_tests/embeddings/test_huggingface_hub.py
Normal file
28
tests/integration_tests/embeddings/test_huggingface_hub.py
Normal file
@ -0,0 +1,28 @@
|
||||
"""Test HuggingFaceHub embeddings."""
|
||||
import pytest
|
||||
|
||||
from langchain.embeddings import HuggingFaceHubEmbeddings
|
||||
|
||||
|
||||
def test_huggingfacehub_embedding_documents() -> None:
|
||||
"""Test huggingfacehub embeddings."""
|
||||
documents = ["foo bar"]
|
||||
embedding = HuggingFaceHubEmbeddings()
|
||||
output = embedding.embed_documents(documents)
|
||||
assert len(output) == 1
|
||||
assert len(output[0]) == 768
|
||||
|
||||
|
||||
def test_huggingfacehub_embedding_query() -> None:
|
||||
"""Test huggingfacehub embeddings."""
|
||||
document = "foo bar"
|
||||
embedding = HuggingFaceHubEmbeddings()
|
||||
output = embedding.embed_query(document)
|
||||
assert len(output) == 768
|
||||
|
||||
|
||||
def test_huggingfacehub_embedding_invalid_repo() -> None:
|
||||
"""Test huggingfacehub embedding repo id validation."""
|
||||
# Only sentence-transformers models are currently supported.
|
||||
with pytest.raises(ValueError):
|
||||
HuggingFaceHubEmbeddings(repo_id="allenai/specter")
|
@ -26,3 +26,21 @@ def test_openai_extra_kwargs() -> None:
|
||||
# Test that if provided twice it errors
|
||||
with pytest.raises(ValueError):
|
||||
OpenAI(foo=3, model_kwargs={"foo": 2})
|
||||
|
||||
|
||||
def test_openai_stop_valid() -> None:
|
||||
"""Test openai stop logic on valid configuration."""
|
||||
query = "write an ordered list of five items"
|
||||
first_llm = OpenAI(stop="3", temperature=0)
|
||||
first_output = first_llm(query)
|
||||
second_llm = OpenAI(temperature=0)
|
||||
second_output = second_llm(query, stop=["3"])
|
||||
# Because it stops on new lines, shouldn't return anything
|
||||
assert first_output == second_output
|
||||
|
||||
|
||||
def test_openai_stop_error() -> None:
|
||||
"""Test openai stop logic on bad configuration."""
|
||||
llm = OpenAI(stop="3", temperature=0)
|
||||
with pytest.raises(ValueError):
|
||||
llm("write an ordered list of five items", stop=["\n"])
|
||||
|
@ -1,9 +1,9 @@
|
||||
"""Integration test for SerpAPI."""
|
||||
from langchain.chains.serpapi import SerpAPIChain
|
||||
from langchain.serpapi import SerpAPIWrapper
|
||||
|
||||
|
||||
def test_call() -> None:
|
||||
"""Test that call gives the correct answer."""
|
||||
chain = SerpAPIChain()
|
||||
chain = SerpAPIWrapper()
|
||||
output = chain.run("What was Obama's first name?")
|
||||
assert output == "Barack Hussein Obama II"
|
@ -28,7 +28,7 @@ class FakeChain(Chain, BaseModel):
|
||||
outputs = {}
|
||||
for var in self.output_variables:
|
||||
variables = [inputs[k] for k in self.input_variables]
|
||||
outputs[var] = " ".join(variables) + "foo"
|
||||
outputs[var] = f"{' '.join(variables)}foo"
|
||||
return outputs
|
||||
|
||||
|
||||
|
@ -43,6 +43,34 @@ def test_loading_from_JSON() -> None:
|
||||
assert prompt == expected_prompt
|
||||
|
||||
|
||||
def test_saving_loading_round_trip(tmp_path: Path) -> None:
|
||||
"""Test equality when saving and loading a prompt."""
|
||||
simple_prompt = PromptTemplate(
|
||||
input_variables=["adjective", "content"],
|
||||
template="Tell me a {adjective} joke about {content}.",
|
||||
)
|
||||
simple_prompt.save(file_path=tmp_path / "prompt.yaml")
|
||||
loaded_prompt = load_prompt(tmp_path / "prompt.yaml")
|
||||
assert loaded_prompt == simple_prompt
|
||||
|
||||
few_shot_prompt = FewShotPromptTemplate(
|
||||
input_variables=["adjective"],
|
||||
prefix="Write antonyms for the following words.",
|
||||
example_prompt=PromptTemplate(
|
||||
input_variables=["input", "output"],
|
||||
template="Input: {input}\nOutput: {output}",
|
||||
),
|
||||
examples=[
|
||||
{"input": "happy", "output": "sad"},
|
||||
{"input": "tall", "output": "short"},
|
||||
],
|
||||
suffix="Input: {adjective}\nOutput:",
|
||||
)
|
||||
few_shot_prompt.save(file_path=tmp_path / "few_shot.yaml")
|
||||
loaded_prompt = load_prompt(tmp_path / "few_shot.yaml")
|
||||
assert loaded_prompt == few_shot_prompt
|
||||
|
||||
|
||||
def test_loading_with_template_as_file() -> None:
|
||||
"""Test loading when the template is a file."""
|
||||
with change_directory():
|
||||
|
9
tests/unit_tests/prompts/test_utils.py
Normal file
9
tests/unit_tests/prompts/test_utils.py
Normal file
@ -0,0 +1,9 @@
|
||||
"""Test functionality related to prompt utils."""
|
||||
from langchain.prompts.example_selector.semantic_similarity import sorted_values
|
||||
|
||||
|
||||
def test_sorted_vals() -> None:
|
||||
"""Test sorted values from dictionary."""
|
||||
test_dict = {"key2": "val2", "key1": "val1"}
|
||||
expected_response = ["val1", "val2"]
|
||||
assert sorted_values(test_dict) == expected_response
|
@ -47,3 +47,17 @@ def test_sql_database_run() -> None:
|
||||
output = db.run(command)
|
||||
expected_output = "[('Harrison',)]"
|
||||
assert output == expected_output
|
||||
|
||||
|
||||
def test_sql_database_run_update() -> None:
|
||||
"""Test commands which return no rows return an empty string."""
|
||||
engine = create_engine("sqlite:///:memory:")
|
||||
metadata_obj.create_all(engine)
|
||||
stmt = insert(user).values(user_id=13, user_name="Harrison")
|
||||
with engine.connect() as conn:
|
||||
conn.execute(stmt)
|
||||
db = SQLDatabase(engine)
|
||||
command = "update user set user_name='Updated' where user_id = 13"
|
||||
output = db.run(command)
|
||||
expected_output = ""
|
||||
assert output == expected_output
|
||||
|
Loading…
Reference in New Issue
Block a user