mteb/FreshStackRetrieval huggingface dataset:
I'm loading pdfs using langchain.document_loaders:
loader = DirectoryLoader( './files/', glob='*.pdf', loader_cls=PyPDFLoader)
then splitted the docs, created the embeddings, stored and loaded them :
docsearch = Chroma.from_documents(texts, embeddings, persist_directory=persist_directory)

...

docsearch = Chroma(persist_directory, embedding_function=embeddings ) 
retriever = docsearch.as_retriever( search_kwargs={"k": 5})
docs = retriever.get_relevant_documents( query )
len( docs)

I'm getting a correct response but I'm getting 0 source documents.
The below def load_documents function is able to load various documents such as .docx, .txt, and .pdf into langchain.  I would also like to be able to load power point documents and found a script here: https://python.langchain.com/docs/integrations/document_loaders that I added to below function.
However, the function is unable to read .pptx files because I am not able to pip install UnstructuredPowerPointLoader.  Can somebody please suggest a way to do this or to augment below function so I can load .pptx files?
Python function follows below:
def load_document(file):
    import os
    name, extension = os.path.splitext(file)

    if extension == '.pdf':
        from langchain.document_loaders import PyPDFLoader
        print(f'Loading {file}')
        loader = PyPDFLoader(file)
    elif extension == '.docx':
        from langchain.document_loaders import Docx2txtLoader
        print(f'Loading {file}')
        loader = Docx2txtLoader(file)
    elif extension == '.txt':
        from langchain.document_loaders import TextLoader
        print(f'Loading {file}')
        loader = TextLoader(file)
    elif extension == '.pptx':
        from langchain_community.document_loaders import UnstructuredPowerPointLoader
        print(f'Loading {file}')
        loader = UnstructuredPowerPointLoader(file)
    else:
        print('Document format is not supported!')
        return None

    data = loader.load()
    return data

The error I am getting is because !pip install unstructured is failing. I tried also tried !pip install -q unstructured["all-docs"]==0.12.0 but was unsuccessful again. Appreciate any help!
I'm trying to do the following simple code:
from transformers import pipeline
import langchain
from langchain.llms import HuggingFacePipeline

model_name = "bert-base-uncased"
task = "question-answering"

hf_pipeline = pipeline(task, model=model_name)

langchain_pipeline = HuggingFacePipeline(hf_pipeline)

I get the following error:

ERROR: TypeError: Serializable.__init__() takes 1 positional argument but 2 were given
LINE: langchain_pipeline = HuggingFacePipeline(hf_pipeline)


Haven't found anything online that actually helped me here


I'm using Databricks with the following cluster:

Runtime: 12.2 LTS ML (includes Apache Spark 3.3.2, Scala 2.12)
Node type: Standard_DS5_v2   56 GB Memory, 16 Cores
Libraries:
The current code I have below works when I use gpt-3.5-turbo-instruct however when I use gpt-4 it doesn't work. I like using this framework because i care about getting only sql code back which the other agents don't do. How do I change it so that I can use other gpt models.
from langchain.chains import create_sql_query_chain
connection_string = ""
db = SQLDatabase.from_uri(connection_string)
llm = OpenAI(temperature=0, verbose=True, model='gpt-4')

seed_prompt = """
Given an input question, create a syntactically correct MySQL SQL query to run.

Question: "Question here"
SQLQuery: "SQL Query to run"

"""

restrictions = """
Never use LIMIT statement, use TOP statement instead.
Format all numeric response ###,###,###,###.
Only return relevant columns to the question.
If a table or column does not exist, return table or column could not be found.
Question: {input}
"""

prompt = seed_prompt + restrictions
PROMPT = PromptTemplate(
    input_variables=["input"], template=prompt
)

database_chain = create_sql_query_chain(llm,db, prompt=PROMPT)
sql_query = database_chain.invoke({"question": x})
print(sql_query)
I'm trying to pass filters to redis retriever to do hybrid search on my embeddings (vector + metadata filtering). The following doesn't work! It fails to pass the filters and filters would always be None:
retriever = redis.as_retriever(
            search_type="similarity_distance_threshold",
            search_kwargs="{'include_metadata': True,'distance_threshold': 0.8,'k': 5}",
            filter="(@launch:{false} @menu_text:(%%chicken%%))"
        )

I found another example and apparently filter expression should be pass as search_kwargs, but I can't figure out what should be the correct syntax. If I do it as follow:
retriever = redis.as_retriever(
            search_type="similarity_distance_threshold",
            "retriever_search_kwargs":"{'include_metadata': True,'distance_threshold': 0.8,'k': 5, 'filter': '@menu_text:(%%chicken%%) @lunch:{true}'}",
}

it generates this search query:
similarity_search_by_vector > redis_query : (@content_vector:[VECTOR_RANGE $distance_threshold $vector] @menu_text:(%%chicken%%) @lunch:{true})=>{$yield_distance_as: distance}
and fails with the following error:
redis.exceptions.ResponseError: Invalid attribute yield_distance_as
Any idea how to fix it?
System Info:
langchain 0.0.346
langchain-core 0.0.10
python 3.9.18
I already installed InstructorEmbedding, but it keeps giving me the error, in jupyter notebook environment using Python 3.12 (I also tried in 3.11). Kernel restarting didn't help.
import torch
from langchain.embeddings import HuggingFaceInstructEmbeddings

DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu"


embedding = HuggingFaceInstructEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": DEVICE})

error:
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
File /opt/conda/lib/python3.11/site-packages/langchain_community/embeddings/huggingface.py:151, in HuggingFaceInstructEmbeddings.__init__(self, **kwargs)
    150 try:
--> 151     from InstructorEmbedding import INSTRUCTOR
    153     self.client = INSTRUCTOR(
    154         self.model_name, cache_folder=self.cache_folder, **self.model_kwargs
    155     )

File /opt/conda/lib/python3.11/site-packages/InstructorEmbedding/__init__.py:1
----> 1 from .instructor import *

File /opt/conda/lib/python3.11/site-packages/InstructorEmbedding/instructor.py:9
      8 from torch import Tensor, device
----> 9 from sentence_transformers import SentenceTransformer
     10 from sentence_transformers.models import Transformer

ModuleNotFoundError: No module named 'sentence_transformers'

The above exception was the direct cause of the following exception:

ImportError                               Traceback (most recent call last)
Cell In[2], line 10
      4 DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu"
      6 #loader = PyPDFDirectoryLoader("aircraft_pdfs")
      7 #docs = loader.load()
      8 #print(len(docs))  # length of all pages together
---> 10 embedding = HuggingFaceInstructEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": DEVICE})

File /opt/conda/lib/python3.11/site-packages/langchain_community/embeddings/huggingface.py:157, in HuggingFaceInstructEmbeddings.__init__(self, **kwargs)
    153     self.client = INSTRUCTOR(
    154         self.model_name, cache_folder=self.cache_folder, **self.model_kwargs
    155     )
    156 except ImportError as e:
--> 157     raise ImportError("Dependencies for InstructorEmbedding not found.") from e

ImportError: Dependencies for InstructorEmbedding not found.

here is the output of pip freeze
transformers==4.37.2
torch==2.2.0
langchain==0.1.6
InstructorEmbedding==1.0.1
...
I was previously using SQLDatabaseChain to connect LLM (Language Model) with my database, and it was functioning correctly with GPT-3.5. However, when attempting the same process with GPT-4, I encountered an error stating "incorrect syntax near 's"
To address this issue, I opted to use SQLDatabaseToolkit and the create_sql_agent function. However, I encountered a problem with this approach as I was unable to pass a prompt. When attempting to include a PromptTemplate in the create_sql_agent argument, it resulted in errors.
ValueError: Prompt missing required variables: {'tool_names', 'agent_scratchpad', 'tools'}
Below is my code:
toolkit = SQLDatabaseToolkit(db=db, llm=llm)

agent_executor = create_sql_agent(
    llm=llm,
    toolkit=toolkit,
    verbose=True,
    prompt=MSSQL_PROMPT,
)
I am trying to use the NebulaGraphStore class from llama_index via from llama_index.graph_stores.nebula import NebulaGraphStore as suggested by the llama_index documentation, but the following error occurred:
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[2], line 1
----> 1 from llama_index.graph_stores.nebula import NebulaGraphStore

ModuleNotFoundError: No module named 'llama_index.graph_stores'

I tried updating llama_index (version 0.10.5) with pip install -U llama-index but it doesn't work. How can I resolve this?
I just upgrade LangChain and OpenAi using below conda install. Then I got below error, any idea how to solve it? Thanks
https://anaconda.org/conda-forge/langchain
conda install conda-forge::langchain
https://anaconda.org/conda-forge/openai
conda install conda-forge::openai
from langchain.agents.agent_toolkits import create_python_agent


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[2], line 1
----> 1 from langchain.agents.agent_toolkits import create_python_agent
      2 from langchain.tools.python.tool import PythonREPLTool
      3 from langchain.llms.openai import OpenAI

File <frozen importlib._bootstrap>:1075, in _handle_fromlist(module, fromlist, import_, recursive)

File c:\Users\yongn\miniconda3\envs\langchain_ai\lib\site-packages\langchain\agents\agent_toolkits\__init__.py:50, in __getattr__(name)
     48 """Get attr name."""
     49 if name in DEPRECATED_AGENTS:
---> 50     relative_path = as_import_path(Path(__file__).parent, suffix=name)
     51     old_path = "langchain." + relative_path
     52     new_path = "langchain_experimental." + relative_path

File c:\Users\test\miniconda3\envs\langchain_ai\lib\site-packages\langchain_core\_api\path.py:30, in as_import_path(file, suffix, relative_to)
     28 if isinstance(file, str):
     29     file = Path(file)
---> 30 path = get_relative_path(file, relative_to=relative_to)
     31 if file.is_file():
     32     path = path[: -len(file.suffix)]

File c:\Users\test\miniconda3\envs\langchain_ai\lib\site-packages\langchain_core\_api\path.py:18, in get_relative_path(file, relative_to)
     16 if isinstance(file, str):
     17     file = Path(file)
---> 18 return str(file.relative_to(relative_to))

File c:\Users\test\miniconda3\envs\langchain_ai\lib\pathlib.py:818, in PurePath.relative_to(self, *other)
    816 if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts):
    817     formatted = self._format_parsed_parts(to_drv, to_root, to_parts)
--> 818     raise ValueError("{!r} is not in the subpath of {!r}"
    819             " OR one path is relative and the other is absolute."
    820                      .format(str(self), str(formatted)))
    821 return self._from_parsed_parts('', root if n == 1 else '',
    822                                abs_parts[n:])

ValueError: 'c:\\Users\\test\\miniconda3\\envs\\langchain_ai\\lib\\site-packages\\langchain\\agents\\agent_toolkits' is not in the subpath of 'c:\\Users\\test\\miniconda3\\envs\\langchain_ai\\lib\\site-packages\\langchain_core' OR one path is relative and the other is absolute.
I am using StructuredParser of Langchain library. I am getting flat dictionary from parser. Please guide me to get a list of dictionaries from output parser.
PROMPT_TEMPLATE = """ 
You are an android developer. 
Parse this error message and provide me identifiers & texts mentioend in error message. 
--------
Error message is {msg}
--------
{format_instructions}
"""

def get_output_parser():
    missing_id = ResponseSchema(name="identifier", description="This is missing identifier.")
    missing_text = ResponseSchema(name="text", description="This is missing text.")

    response_schemas = [missing_id, missing_text]
    output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
    return output_parser


def predict_result(msg):
    model = ChatOpenAI(open_api_key="", openai_api_base="", model="llama-2-70b-chat-hf", temperature=0, max_tokens=2000)
    output_parser = get_output_parser()
    format_instructions = output_parser.get_format_instructions()
    
    prompt = ChatPromptTemplate.from_template(template=PROMPT_TEMPLATE)
    message = prompt.format_messages(msg=msg, format_instructions=format_instructions)
    response = model.invoke(message)

    response_as_dict = output_parser.parse(response.content)
    print(response_as_dict)


predict_result("ObjectNotFoundException AnyOf(AllOf(withId:identifier1, withText:text1),AllOf(withId:identifier2, withText:text1),AllOf(withId:identifier3, withText:text1))")

The output I get is
{
    "identifier":"identifier1",
    "text":"text1"
}


Expected output is
[
    {
        "identifier":"identifier1",
        "text":"text1"
    },
    {
        "identifier":"identifier2",
        "text":"text1"
    },
    {
        "identifier":"identifier3",
        "text":"text1"
    }
]

How to specify such nested JSON in OutputParser
Hello i am trying to run this following code but i am getting an error;
from langchain.schema import BaseOuputParser

Error;

ImportError: cannot import name 'BaseOuputParser' from
'langchain.schema'

My langchain version is ; '0.1.7'
I'm trying to understand what the correct strategy is for storing and using embeddings in a vector database, to be used with an LLM.  If my goal is to reduce the amount of work the LLM has to do when generating a response, (So you can think of a RAG implementation where I've stored text, embeddings I've created using an LLM, and metadata about the text.)  I'm then trying to generate responses using say openai model from queries about the data, and I don't want to have to spend a bunch of money and time chunking up the text and creating embeddings for it every time I want to answer a query about it.
If I create a vector database, for example a chroma database and I use an LLM to create embeddings for a corpus I have.  I save those embeddings into the vector database, along with the text and metadata.  Would the database use those embeddings I created to find the relevant text chunks, or would it make more sense for the vector database to use it's own query process to find the relevant chunks (not using the embeddings the LLM created)?
Also do I want to pass the embeddings from the vector database to the LLM to generate the response, or do I pass the text that the vectore database found was most relevant to the LLM along with original text query so the LLM can then generate a response?
I'm creating a QA bot with RAG and aiming to provide the specific documents from which the answers are extracted.
Retrieval QA uses k documents which are semantically similar to query to generate the answer. The answer need not be in all the k documents, how can we know which documents out of the k documents the answer is extracted from?
How can we know which of those source documents that LLM extracted the answer from?
Been stuck on trying to add variables to the prompt. template using this example from Langchain Docs as a starting point.
ValidationError: 1 validation error for ConversationChain __root__ Got unexpected prompt input variables. The prompt expects ['history', 'input', 'daily_context'], but got ['history'] as inputs from memory, and input as the normal input key. (type=value_error

Any suggestions here to get past the validation?
daily_context = "It is a Saturday, the office is open."

template = """You are Jane's Boss.  

Here is some context about today: {daily_context}

Current conversation:
{history}
Jane: {input}
AI:"""
PROMPT = PromptTemplate(input_variables=[ "history", "input", "daily_context"], template=template)

llm = OpenAI(temperature=0.4, model="gpt-3.5-turbo-instruct")
conversation = ConversationChain(
 prompt=PROMPT,
 llm=llm,
 verbose=True,
 memory=ConversationBufferMemory(),
)
From the langchain documentation - Per-User Retrieval

When building a retrieval app, you often have to build it with multiple users in mind. This means that you may be storing data not just for one user, but for many different users, and they should not be able to see eachother’s data. This means that you need to be able to configure your retrieval chain to only retrieve certain information.

The documentation has an example implementation using PineconeVectorStore. Does chromadb support multiple users? If yes, can anyone help with an example of how the per-user retrieval can be implemented using the open source ChromaDB?
Not a coding question, but a documentation omission that is nowhere mentioned online at this point.   When using the Langchain CSVLoader, which column is being vectorized via the OpenAI embeddings I am using?
I ask because viewing this code below, I vectorized a sample CSV, did searches (on Pinecone) and consistently received back DISsimilar responses.  How do know which column Langchain is actually identifying to vectorize?
loader = CSVLoader(file_path=file, metadata_columns=['col2', 'col3', 'col4','col5'])
langchain_docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=100)
docs = text_splitter.split_documents(langchain_docs)
for doc in docs:
    doc.metadata.pop('source')
    doc.metadata.pop('row')
my_index = pc_store.from_documents(docs, embeddings, index_name=PINECONE_INDEX_NAME)

I am assuming the CSVLoader is then identifying col1 to vectorize.  But, searches of Pinecone are terrible, leading me to think some other column is being vectorized.
I built a QnA App with Flowise.
Until now I used the ChatOpenAI node together with the OpenAI Embeddings.
Today, I wanted to try the Anthropic Claude LLM, but couldnt find specific Anthropic Embeddings. So, curiously, I used the OpenAI Embeddings just to see what would happen.
I expected the response to not work, or to be complete gibberish because I thought Embeddings were model specific?
But facscinatingly I got a perfect response.
Can someone please explain how this is possible? I thought embeddings had to be learned model specificaly? My complete understanding of embeddings is shattered.
This is my Flowise chatflow:

Edit:
Is it possible, that the documents are embedded by openai, and my prompts are also embedded with openai, to retrieve the texts with highest similarity? Then the texts and my prompt are both passed to claude?
I have successfully connected to a Redshift database like below and got all the table names;
conn = psycopg2.connect(host,db,port,username,password)
cursor.execute("SELECT tablename FROM pg_tables GROUP BY tablename ORDER BY tablename")

However, when I connect using langchain and sqlalchemy like below, get_usable_table_names returns few of many tables in the database;
pg_url = f"postgresql+psycopg2://{db_user}:{db_password}@{db_host}:{port_}/{db_}"
db_engine = create_engine(pg_url)
db = SQLDatabase(db_engine)
llm = OpenAI(temperature=0.0, openai_api_key=OPENAI_API_KEY, model='gpt-3.5-turbo')

table_names = "\n".join(db.get_usable_table_names())

Anyone has any suggestions on what might be the issue?
I have tried querying a missing table by;
db.run("SELECT * FROM db_schema.missing_table_name") 

and this works. However, I need SQLDatabase from langchain.sql_database module to detect the tables right without specifying one by one. (Because I would like to Chat With Sql Database Using Langchain & OpenAI)
I am using the below code and for the same question, it return different results, is there any way to fix that?
from langchain.chains import create_sql_query_chain
from langchain_openai import ChatOpenAI
from langchain_community.utilities import SQLDatabase
import os

def return_query(question)
   db = SQLDatabase.from_uri(os.getenv("POSTGRES_URL"))
   llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
   chain = create_sql_query_chain(llm, db)
   response = chain.invoke({"question": question})
   return response

Example my question is "create table student" and i get the below responses on re-trying the same code:

Response1: This table does not exist in the provided database schema.
Response2: SELECT * FROM information_schema.tables WHERE table_name = 'student' LIMIT 1;
Response3: This question cannot be answered directly using the existing tables provided in the database schema. To create a new table named "student", you can use the following SQL query:

CREATE TABLE student (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT NOT NULL,
    age INTEGER,
    major TEXT
);
I'm trying to use the Langchain ReAct Agents and I want to give them my pinecone index for context. I couldn't find any interface that let me provide the LLM that uses the ReAct chain my vector embeddings as well.
Here I set up the LLM and retrieve my vector embedding.
llm = ChatOpenAI(temperature=0.1, model_name="gpt-4")
retriever = vector_store.as_retriever(search_type='similarity', search_kwargs={'k': k})

Here I start my ReAct Chain.
prompt = hub.pull("hwchase17/structured-chat-agent")
agent = create_structured_chat_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = agent_executor.invoke(
    {
        "input": question,
        "chat_history": chat_history
    }
)

Before using the ReAct Agent, I used the vector embedding like this.
crc = ConversationalRetrievalChain.from_llm(llm, retriever)
result = crc.invoke({'question': systemPrompt, 'chat_history': chat_history})
chat_history.append((question, result['answer']))

Is there any way to combine both methods and have a ReAct agent that also uses vector Embeddings?
I am trying to create a AI chatbot backend with langchain and fastAPI. I've managed to generate an output based on a user query. The bot embeds a context hardcoded.
However the response I got includes everything (the query, the full template as well as the actual bot's answer). Is there any way to get the bot's answer as answer ?
Thank you
template = """
You are Bot, a AI bot to help user of my portfolio if they need to. Always be thankfull with the user from showing interest to my portfolio.
Depending on user's question you need to point them to the right section of the portfolio. 
If they want to contact me they can use the contact form or visit one of my social media via the links

My name is John Doe and this is my personal portfolio where I display my interests:
    - chess with my chess.com stats
    - running with my strava stats result
    - some pictures showing my accomplishments and my passion for travels and mountains
    - a contact form to reach me out
    - links to my different social media (facebook, linkedin, github, chess.com and strava)

User query : {question}
Bot's answer :"""

app = FastAPI()

@app.post("/conversation")
async def read_conversation(query:str):

    repo_id = "mistralai/Mistral-7B-Instruct-v0.2"

    llm1 = HuggingFaceHub(
    repo_id=repo_id, 
    model_kwargs={"temperature" : 0.7}
    )

    prompt = PromptTemplate(
        input_variables=["question"], template=template
    )
    chain = LLMChain(llm=llm1, prompt=prompt)
    response = chain.invoke({"question":query})

    return {"response" : response}