mteb/FreshStackRetrieval huggingface dataset:
using LangChain and OpenAI, how can I have the model return a specific default response? for instance, let's say I have these statement/responses
Statement: Hi, I need to update my email address.
Answer: Thank you for updating us. Please text it here.

Statement: Hi, I have a few questions regarding my case. Can you call me back?
Answer: Hi. Yes, one of our case managers will give you a call shortly. 

if the input is similar to one of the above statements, I would like to have OpenAI respond with the specific answer.
I'm trying to run a chain in LangChain with memory and multiple inputs. The closest error I could find was was posted here, but in that one, they are passing only one input.
Here is the setup:
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory

llm = OpenAI(
    model="text-davinci-003",
    openai_api_key=environment_values["OPEN_AI_KEY"], # Used dotenv to store API key
    temperature=0.9,
    client="",
)

memory = ConversationBufferMemory(memory_key="chat_history")

prompt = PromptTemplate(
    input_variables=[
        "text_one",
        "text_two",
        "chat_history"
    ],
    template=(
        """You are an AI talking to a huamn. Here is the chat
        history so far:

        {chat_history}

        Here is some more text:

        {text_one}

        and here is a even more text:

        {text_two}
        """
    )
)

chain = LLMChain(
    llm=llm,
    prompt=prompt,
    memory=memory,
    verbose=False
)

When I run
output = chain.predict(
    text_one="Hello",
    text_two="World"
)

I get ValueError: One input key expected got ['text_one', 'text_two']
I've looked at this stackoverflow post, which suggests to try:
output = chain(
    inputs={
        "text_one" : "Hello",
        "text_two" : "World"
    }
)

which gives the exact same error. In the spirit of trying different things, I've also tried:
output = chain.predict( # Also tried .run() here
    inputs={
        "text_one" : "Hello",
        "text_two" : "World"
    }
)

which gives Missing some input keys: {'text_one', 'text_two'}.
I've also looked at this issue on the langchain GitHub, which suggests to do pass the llm into memory, i.e.
# Everything the same except...
memory = ConversationBufferMemory(llm=llm, memory_key="chat_history") # Note the llm here

and I still get the same error. If someone knows a way around this error, please let me know. Thank-you.
Is there a way to force a LangChain agent to always use a tool?
(my specific use case: I need the agent to look for information in my database as opposed to generally accessible information and my tool searches through my database)
I am trying to make an LLM model that answers questions from the panda's data frame by using Langchain agent.
However, when the model can't find the answers from the data frame, I want the model to google the question and try to get the answers from the website.
I tried different methods but I could not incorporate the two functions together.
I currently have a dataset in csv file, and I converted it into the pandas dataframe.
After that, I have created the agent as shown below.
agent = create_pandas_dataframe_agent(OpenAI(temperature=1), df, verbose=True)
I am a beginner who just tried to use LLM model. Any help or support would be appreciated!
I am using the DirectoryLoader with Langchain on HuggingFace (Gradio SDK) like so from my folder named "data":
from langchain.document_loaders import DirectoryLoader  
  
loader = DirectoryLoader('./data/')  
raw_documents = loader.load() 

but get the following error:
ImportError: partition_docx is not available. Install the docx dependencies with pip install "unstructured[docx]"
Does anyone have any insight as to why this error is being given? Nothing pops up for me on a web search for this error.
Thanks in advance! Apologies if more context is needed, just getting into python and I am very novice.
I'm currently working on developing a chatbot powered by a Large Language Model (LLM), and I want it to provide responses based on my own documents. I understand that using a fine-tuned model on my documents might not yield direct responses, so I'm exploring the concept of Retrieval-Augmented Generation (RAG) to enhance its performance.
In my research, I've come across two tools, Langchain and LlamaIndex, that seem to facilitate RAG. However, I'm struggling to understand the main differences between them. I've noticed that some tutorials and resources use both tools simultaneously, and I'm curious about why one might choose to use one over the other or when it makes sense to use them together.
Could someone please provide insights into the key distinctions between Langchain and LlamaIndex for RAG, and when it is beneficial to use one tool over the other or combine them in chatbot development?
I am trying to use my llama2 model (exposed as an API using ollama). I want to chat with the llama agent and query my Postgres db (i.e. generate text to sql). I was able to find langchain code that uses open AI to do this. However, I am unable to find anything out there which fits my situation.
Any pointers will be of great help.
Code with openai
# Create connection to postgres
import psycopg2  # Import the library

database = 'postgres'
username = 'postgres'
password = 'password'
server = 'localhost'
port = '5432'

# Establish the connection
conn = psycopg2.connect(
    dbname=database,
    user=username,
    password=password,
    host=server,
    port=port
)

db = SQLDatabase.from_uri(
    "postgresql://postgres:password@localhost:5432/postgres")
toolkit = SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0))

agent_executor = create_sql_agent(
    llm=OpenAI(temperature=0),
    toolkit=toolkit,
    verbose=True,
    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
)

agent_executor.run("Describe the transaction table")

I want to make the above code work for my llama2 model exposed via an API at localhost:11434/api/generate
I have been trying to use

Chromadb version 0.4.8
Langchain version 0.0.276

with SentenceTransformerEmbeddingFunction as shown in the snippet below.
from langchain.vectorstores import Chroma
from chromadb.utils import embedding_functions

# other imports
embedding = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="all-MiniLM-L6-v2")

However, it throws the following error.
RuntimeError: Your system has an unsupported version of sqlite3. Chroma requires sqlite3 >= 3.35.0.

Funfact is I do have the required sqlite3 (3.43.0) available which I can validate using the command sqlite3 --version.
Would appreciate any help. Thank you.
I have the following piece of code:
if file.filename.lower().endswith('.pdf'):
                        pdf = ep.PDFLoad(file_path)  # this is the loader from langchain
                        doc = pdf.load()
                        archivo = crear_archivo(doc, file)

Inside crear_archivo function I am splitting the document and sending it to the Weaviate.add_documents:
   cliente = db.NewVect()  # This one creates the weaviate.client
    text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
    docs = text_splitter.split_documents(document)

    embeddings = OpenAIEmbeddings()
    return Weaviate.add_documents(docs, embeddings, client=client, weaviate_url=EnvVect.Host, by_text=False,      index_name="LangChain") 
# using this instead of from_documents since I don't want to initialize a new vectorstore         

# Some more logic to save the doc to another database

Whenever I try to run the code it breaks during the Weaviate.add_documents() function prompting the following error:
'tuple' object has no attribute 'page_content'.
I tried to check the type of docs, but that doesn't seem wrong since it returns a List[Document] which is the same type the function accepts.
How can I make it work? I kind of followed this approach but the difference is I am loading files such as PDF, txt etc.
Recently, while developing a Streamlit app, the app frequently crashes and requires manual rebooting.
After spending some time, I identified the issue as "exceeding RAM". The free version of RAM is only 1GB, and my app easily surpasses this limit when multiple users are using it simultaneously.
Application of the App

Using langchain to build a document GPT.
Users upload PDFs and start asking questions.

Main Problematic Code
Complete code from Github
app.py
model = None

doc_container = st.container()
with doc_container:
    # when user upload pdf base on upload_and_process_pdf()
    # the create_doc_gpt() can execute successfully
    docs = upload_and_process_pdf()
    model = create_doc_gpt(docs)
    del docs
    st.write('---')

def create_doc_gpt(docs):
    if not docs:
        return

    ... instance docGPT which will use HuggingFaceEmbedding

What I've Tried
I attempted to identify where the issue in the code lies and whether optimization is possible. I conducted the following experiments:

Used Windows Task Manager's detailed view.

Executed the app (streamlit run app.py) and simultaneously identified its PID, observing memory usage.

When opening the app, memory usage occupied 150,000 KB.

Based on the simplified code above, after uploading a PDF, the docGPT instance (my model) is instantiated. At this point, memory rapidly spikes to 1,000,000 KB. I suspect this is due to HuggingFaceEmbedding causing this. (When I switched to a lighter embedding, memory decreased significantly)

Since memory's main source is the model instance, but when I re-upload the same PDF, memory increases again to 1,750,000 KB. This seems like two models are occupying memory.

Additionally, I have attempted to repeatedly upload the same PDF on my app. After uploading the 8000KB file approximately 4 times, the app crashes.


Question
How should I correctly release the initially instantiated model?
If I use st.cache_resource to decorate create_doc_gpt(docs), I have a few points of confusion as follows:

When the same user uploads the first PDF, the embedding is performed, and the model is returned. At this point, does the app create a cache and occupy memory? If the user uploads a new PDF again, will the app go through embedding and returning the model, creating a new cache and occupying memory again?

If the assumption in #1 is correct, can I use the ttl and max_entries parameters to avoid excessive caching?

If the assumptions in #1 and #2 are correct, when there are two users simultaneously, and my max_entries is set to 2, will the cached models they create be counted separately?



I'm unsure if this type of question is appropriate to ask here. If it's against the rules, I'm willing to delete the post and seek help elsewhere.
I currently trying to implement langchain functionality to talk with pdf documents.
I have a bunch of pdf files stored in Azure Blob Storage. I am trying to use langchain PyPDFLoader to load the pdf files to the Azure ML notebook. However, I am not being able to get it done. If I have the pdf stored locally, it is no problem, but to scale up I have to connect to the blob store. I have not really found any documents on langchain website or azure website. Wondering, if any of you is having similar problem.
Thank you
Below is an example of code i am trying:
from azureml.fsspec import AzureMachineLearningFileSystem
fs = AzureMachineLearningFileSystem("<path to datastore>")

from langchain.document_loaders import PyPDFLoader
with fs.open('*/.../file.pdf', 'rb') as fd:
    loader = PyPDFLoader(document)
    data = loader.load()

Error: TypeError: expected str, bytes or os.PathLike object, not StreamInfoFileObject

Another example tried:
from langchain.document_loaders import UnstructuredFileLoader
with fs.open('*/.../file.pdf', 'rb') as fd:
    loader = UnstructuredFileLoader(fd)
documents = loader.load() 

Error: TypeError: expected str, bytes or os.PathLike object, not StreamInfoFileObject
from langchain.document_loaders import TextLoader
# Create the TextLoader object using the file path
Loader = tl('data.txt')

I want to use a langchain with a string instead of a txt file, is this possible?
def get_response(query):
    #print(query)
    result = index.query(query)
    result = str(result)
I am trying to initialise a langchain with a pre-trained custom model of my own rather than use one of Google's base models. When I run the code with text-bison, it works as expected but when I run it with my own model, I get the following exception:
_InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
    status = StatusCode.INVALID_ARGUMENT
    details = "Request contains an invalid argument."
    debug_error_string = "UNKNOWN:Error received from peer ipv4:x.x.x.x:443 {grpc_message:"Request contains an invalid argument.", grpc_status:3, created_time:"2023-09-07T06:14:11.519783678+00:00"}"

My custom model is supervised trained on text-bison. Here is my code:
#llm = VertexAI(model_name="text-bison@001", max_output_tokens=1024)
llm = VertexAI(model_name="projects/xxx/locations/us-central1/models/xxx", max_output_tokens=1024)
conversation_buf = ConversationChain(
    llm=llm,
    memory=ConversationBufferMemory()
)
I am following this tutorial from langchain official documentation here were I try to track the number of tokens while usage. However, I wanted to use gpt-3.5-turbo instead of text-davinci-003 so I changed the LLM class used from OpenAI to ChatOpenAI but this a Value Error of unsupported message type
Here is the code snippet:
from langchain.chat_models import ChatOpenAI
from langchain.callbacks import get_openai_callback

os.environ['OPENAI_API_KEY'] = "OPENAI-API-KEY"

llm = ChatOpenAI(
  model_name='gpt-3.5-turbo-16k',
  temperature=0.0
)

with get_openai_callback() as cb:
    result = llm("Tell me a joke")
    print(cb)

Getting this error:
ValueError: Got unsupported message type: T
Why changing the class from OpenAI to ChatOpenAI gives this error? How to solve?
I am trying to deploy a Llama 2 model for text generation inference using Sagemaker and LangChain. I am writing code in Notebook instances and deploying SageMaker instances from the code.
I followed the documentation from https://python.langchain.com/docs/integrations/llms/sagemaker. I used the following code to create a chain for question answering:
from langchain.docstore.document import Document
example_doc_1 = """
Peter and Elizabeth took a taxi to attend the night party in the city. While in the party, Elizabeth collapsed and was rushed to the hospital.
Since she was diagnosed with a brain injury, the doctor told Peter to stay besides her until she gets well.
Therefore, Peter stayed with her at the hospital for 3 days without leaving.
"""

docs = [
    Document(
        page_content=example_doc_1,
    )
]

from typing import Dict

from langchain import PromptTemplate, SagemakerEndpoint
from langchain.llms.sagemaker_endpoint import LLMContentHandler
from langchain.chains.question_answering import load_qa_chain
import json

query = """How long was Elizabeth hospitalized?
"""

prompt_template = """Use the following pieces of context to answer the question at the end.

{context}

Question: {question}
Answer:"""
PROMPT = PromptTemplate(
    template=prompt_template, input_variables=["context", "question"]
)


class ContentHandler(LLMContentHandler):
    content_type = "application/json"
    accepts = "application/json"

    def transform_input(self, prompt: str, model_kwargs: Dict) -> bytes:
        input_str = json.dumps({prompt: prompt, **model_kwargs})
        return input_str.encode("utf-8")

    def transform_output(self, output: bytes) -> str:
        response_json = json.loads(output.read().decode("utf-8"))
        return response_json[0]["generated_text"]


content_handler = ContentHandler()

chain = load_qa_chain(
    llm=SagemakerEndpoint(
        endpoint_name="XYZ",
        credentials_profile_name="XYZ",
        region_name="XYZ",
        model_kwargs={"temperature": 1e-10},
        content_handler=content_handler,
    ),
    prompt=PROMPT,
)

chain({"input_documents": docs, "question": query}, return_only_outputs=True)

But I got an error
ValueError: Error raised by inference endpoint: 
An error occurred (ModelError) when calling the InvokeEndpoint operation: 
Received client error (422) from primary with message 
"Failed to deserialize the JSON body into the target type: missing field `inputs` at line 1 column 966".

In multiple tutorials there isn't any inputs field. I have no idea if they updated the documentation, which I have been referring to but can't resolve this problem.
My question is:

Why am I getting this error and how can I fix it?
What am I missing in my code or configuration?
Any help or guidance would be appreciated. Thanks in advance.
Currently I have managed to make a web interface to chat with a single PDF document using langchain as a framework, OpenAI as an LLM and Pinecone as a vector store. However, when I wanted to introduce new documents (5 new documents) PDF to the vecotres store, I realized that the information is different from the first document.
I have thought about introducing the resulting embeddings of all the pdf documents to Pinecone. But I have a doubt about whether the information can be crossed when specific information is requested from only one PDF document.
So I'm thinking that another way could be to add some selectors in the same web interface so that the user can choose from the PDF they want to obtain answers from. and thus the information is directed to the specific PDF. But perhaps the user's interaction with the web interface would not be so automatic.
This is why I want to find a way to send all pdf documents to pinecone, and perhaps in the vector store itself add an index for each document or add more collections. I appreciate if anyone has worked on something similar and can give me advice to continue with my task.
With the
 docs = (db.similarity_search(query='some query here'))
method to output single or multiple documents of the deeplake vectorstore. Is there a method to output all documents?
Because my documents are structured like this:
page_content='256 128 256zM208 160c-8,836 0-16-...
384C234.5 384 256 362.5 256 336C256 309.5 234.5 288 208' 
metadata={'source':'chatbot/app/solid.min.js','file_name':'solid.min.js'}

And I would genre all documents whose metadata.file_name corresponds to a particular file. Unfortunately I can't find any recordings for this and that's why I'm asking here for experience.
I have this requirement, where i want to create a knowledge retriver which will call the API to get the closest matching information, I know that we have these integrations in langchain with multiple vector stores, but we have requirement were we have to call the API to find the closest matching document how can we create our custom retriver in langchain which will call this API to get the nearest matching informtaion
I'm trying to build the custom retriver in langchain but still not able figure it out
As shown in LangChain Quickstart, I am trying the following Python code:
from langchain.prompts.chat import ChatPromptTemplate
template = "You are a helpful assistant that translates {input_language} to {output_language}."
human_template = "{text}"

chat_prompt = ChatPromptTemplate.from_messages([
    ("system", template),
    ("human", human_template),
])

chat_prompt.format_messages(input_language="English", output_language="French", text="I love programming.")

But when I run the above code, I get the following error:
Traceback (most recent call last):
   File "/home/yser364/Projets/SinappsIrdOpenaiQA/promptWorkout.py", line 6, in <module>
     chat_prompt = ChatPromptTemplate.from_messages([
   File "/home/yser364/.local/lib/python3.10/site-packages/langchain/prompts/chat.py", line 220, in from_messages
     return cls(input_variables=list(input_vars), messages=messages)
   File "/home/yser364/.local/lib/python3.10/site-packages/langchain/load/serializable.py", line 64, in __init__
     super().__init__(**kwargs)
   File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
 pydantic.error_wrappers.ValidationError: 4 validation errors for ChatPromptTemplate
 messages -> 0
   value is not a valid dict (type=type_error.dict)
 messages -> 0
   value is not a valid dict (type=type_error.dict)
 messages -> 1
   value is not a valid dict (type=type_error.dict)
 messages -> 1
   value is not a valid dict (type=type_error.dict)

I use Python 3.10.12.
I am making a chatbot which accesses an external knowledge base docs. I want to get the relevant documents the bot accessed for its answer, but this shouldn't be the case when the user input is something like "hello", "how are you", "what's 2+2", or any answer that is not retrieved from the external knowledge base docs. In this case, I want
retriever.get_relevant_documents(query) or any other line to return an empty list or something similar.
import os
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains import ConversationalRetrievalChain 
from langchain.memory import ConversationBufferMemory
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate

os.environ['OPENAI_API_KEY'] = ''

custom_template = """
This is conversation with a human. Answer the questions you get based on the knowledge you have.
If you don't know the answer, just say that you don't, don't try to make up an answer.
Chat History:
{chat_history}
Follow Up Input: {question}
"""
CUSTOM_QUESTION_PROMPT = PromptTemplate.from_template(custom_template)

llm = ChatOpenAI(
    model_name="gpt-3.5-turbo",  # Name of the language model
    temperature=0  # Parameter that controls the randomness of the generated responses
)

embeddings = OpenAIEmbeddings()

docs = [
    "Buildings are made out of brick",
    "Buildings are made out of wood",
    "Buildings are made out of stone",
    "Buildings are made out of atoms",
    "Buildings are made out of building materials",
    "Cars are made out of metal",
    "Cars are made out of plastic",
  ]

vectorstore = FAISS.from_texts(docs, embeddings)

retriever = vectorstore.as_retriever()

memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

qa = ConversationalRetrievalChain.from_llm(
    llm,
    retriever,
    condense_question_prompt=CUSTOM_QUESTION_PROMPT,
    memory=memory
)

query = "what are cars made of?"
result = qa({"question": query})
print(result)
print(retriever.get_relevant_documents(query))

I tried setting a threshold for the retriever but I still get relevant documents with high similarity scores. And in other user prompts where there is a relevant document, I do not get back any relevant documents.
retriever = vectorstore.as_retriever(search_type="similarity_score_threshold", search_kwargs={"score_threshold": .9})
I have the following LangChain code that checks the chroma vectorstore and extracts the answers from the stored docs - how do I incorporate a Prompt template to create some context , such as the following:
sales_template = """You are customer services and you need to help people.
{context}
Question: {question}"""
SALES_PROMPT = PromptTemplate(
    template=sales_template, input_variables=["context", "question"]
)

How do I incorporate the above into the below?
#Embedding Text Using Langchain
from langchain.embeddings import SentenceTransformerEmbeddings
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")

# Creating Vector Store with Chroma DB
from langchain.vectorstores import Chroma
#db = Chroma.from_documents(docs, embeddings)
db = Chroma(persist_directory="./chroma_db", embedding_function=embeddings)
# docs = db3.similarity_search(query)
# print(docs[0].page_content)

#Using OpenAI Large Language Models (LLM) with Chroma DB
import os
os.environ["OPENAI_API_KEY"] =  'sk-12345678910'

from langchain.chat_models import ChatOpenAI
model_name = "gpt-3.5-turbo"
llm = ChatOpenAI(model_name=model_name)

#Extracting Answers from Documents

from langchain.chains.question_answering import load_qa_chain
chain = load_qa_chain(llm, chain_type="stuff",verbose=True)

query = "What does Neil do for work?"
matching_docs = db.similarity_search(query)
answer =  chain.run(input_documents=matching_docs, question=query)
print(answer)