Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stevengonsalvez/b007bac8d3b9fbef6dbbbda145b2745b to your computer and use it in GitHub Desktop.
Save stevengonsalvez/b007bac8d3b9fbef6dbbbda145b2745b to your computer and use it in GitHub Desktop.
The code snippet creates a retrieval chain for text-based knowledge using a combination of language models and vector stores. It then creates an agent with conversational capabilities and executes a question using the agent and retrieval chain to obtain an answer.

Text Retrieval Agent with Conversational Agent Executor

Preview:
from langchain import SQLDatabase, SQLDatabaseChain
from langchain.prompts.prompt import PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferWindowMemory
from langchain.chains import ConversationChain, LLMChain
from langchain.embeddings import OpenAIEmbeddings
from langchain.chains import RetrievalQAWithSourcesChain, RetrievalQA
from langchain.vectorstores import Pinecone
from langchain.agents import Agent, Tool, AgentType, AgentOutputParser, AgentExecutor, initialize_agent
from langchain.agents.conversational.output_parser import ConvoOutputParser
from langchain.agents.conversational.prompt import SUFFIX
from pydantic import Field
from langchain.tools.base import BaseTool
from langchain.base_language import BaseLanguageModel
from langchain.callbacks.base import BaseCallbackManager

def text_retrieval_chain():
    # main retrieval chain class
    class RetrievalChain:
        def __init__(self, llm, retriever):
            self.chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)

        def run(self, prompt):
            response = self.chain(prompt)
            return response['result']

    # vectorstore
    index_name = "testing"
    vectorstore = Pinecone.from_existing_index(
        index_name=index_name, namespace="articlefeeds", embedding=embeddings
    )
    retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
    return RetrievalChain(llm=tool_llm, retriever=retriever)


# retrieval chain for article/RSS feed
text_retrieval_chain = text_retrieval_chain()
# Create the tools list
tools = [
    Tool(
        name="Text Retrieval",
        func=text_retrieval_chain.run,
        description="Useful for when you need to find textual knowledge.",
    ),
]

# create agent
agent = ConversationalAgent.from_llm_and_tools(llm=llm, tools=tools)

agent_memory = ConversationBufferWindowMemory(k=1, memory_key="chat_history", return_messages=True)
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, memory=agent_memory, verbose=verbose)

answer = agent_executor.run(question)
Associated Context
Type Code Snippet ( .py )
Associated Tags langchain SQLDatabase SQLDatabaseChain PromptTemplate ChatOpenAI ConversationBufferWindowMemory LLMChain OpenAIEmbeddings RetrievalQAWithSourcesChain Predefined Index tensorflow google-colaboratory pyyaml
📝 Custom Description Snippet saved via the Pieces Web Extension
💡 Smart Description The code snippet defines a function called "text_retrieval_chain" that takes in an SQL database, chat models, and the LLM chain. It then creates a tool for article/RSS feed using OpenAI's
The code snippet creates a retrieval chain for text-based knowledge using a combination of language models and vector stores. It then creates an agent with conversational capabilities and executes a question using the agent and retrieval chain to obtain an answer.
🔎 Suggested Searches Python code for creating a text retrieval chain using langchain
Related Links langchain-ai/langchain#10306
https://www.google.com/search?q=parquet+python&oq=parquet+python&gs_lcrp=EgZjaHJvbWUyCQgAEEUYORiABDIHCAEQABiABDIHCAIQABiABDIHCAMQABiABDIICAQQABgWGB4yCAgFEAAYFhgeMggIBhAAGBYYHjIICAcQABgWGB4yCAgIEAAYFhgeMggICRAAGBYYHtIBCDQyMDZqMGo5qAIAsAIA&sourceid=chrome&ie=UTF-8
https://arrow.apache.org/docs/python/parquet.html
https://sparkbyexamples.com/python/python-return-tuple-from-function/
https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html
https://machinelearningmastery.com/what-are-word-embeddings/
https://www.programiz.com/python-programming/methods/built-in/isinstance
https://pypi.org/project/langchain/
https://www.geeksforgeeks.org/
https://docs.python.org/3/library/sqlite3.html
https://docs.python.org/3/library/typing.html
https://stackoverflow.com/
Related People steven gonsalvez
Sensitive Information No Sensitive Information Detected
Shareable Link https://stevengonsalvez.pieces.cloud/?p=39424fbebe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment