mteb/FreshStackRetrieval huggingface dataset:
I'm trying to create a Qdrant vectorsore  and add my documents.

My embeddings  are based on OpenAIEmbeddings
the QdrantClient is local for my case
the collection that I'm creating has the
VectorParams as such: VectorParams(size=2000, distance=Distance.EUCLID)

I'm getting the following error:
ValueError: could not broadcast input array from shape (1536,) into shape (2000,)
I understand that my error is how I configure the vectorParams, but I don't undertsand how these values need to be calculated.
here's my complete code:
import os
from typing import List

from langchain.docstore.document import Document
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Qdrant, VectorStore
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams

def load_documents(documents: List[Document]) -> VectorStore:
    """Create a vectorstore from documents."""
    collection_name = "my_collection"
    vectorstore_path = "data/vectorstore/qdrant"
    embeddings = OpenAIEmbeddings(
        model="text-embedding-ada-002",
        openai_api_key=os.getenv("OPENAI_API_KEY"),
    )
    qdrantClient = QdrantClient(path=vectorstore_path, prefer_grpc=True)
    qdrantClient.create_collection(
        collection_name=collection_name,
        vectors_config=VectorParams(size=2000, distance=Distance.EUCLID),
    )
    vectorstore = Qdrant(
        client=qdrantClient,
        collection_name=collection_name,
        embeddings=embeddings,
    )
    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size=1000,
        chunk_overlap=200,
    )

    sub_docs = text_splitter.split_documents(documents)
    vectorstore.add_documents(sub_docs)

    return vectorstore

Any ideas on how I should configure the vector params properly?
I am using the SQL Database Agent to query a postgres database. I want to use gpt 4 or gpt 3.5 models in the OpenAI llm passed to the agent, but it says I must use ChatOpenAI. Using ChatOpenAI throws parsing errors.
The reason for wanting to switch models is reduced cost, better performance and most importantly - token limit. The max token size is 4k for 'text-davinci-003' and I need at least double that.
Here is my code
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain.agents import create_sql_agent
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
import os

os.environ["OPENAI_API_KEY"] = ""
db = SQLDatabase.from_uri(
    "postgresql://<my-db-uri>",
    engine_args={
        "connect_args": {"sslmode": "require"},
    },
)

llm = ChatOpenAI(model_name="gpt-3.5-turbo")
toolkit = SQLDatabaseToolkit(db=db, llm=llm)

agent_executor = create_sql_agent(
    llm=llm,
    toolkit=toolkit,
    verbose=True,
)

agent_executor.run("list the tables in the db. Give the answer in a table json format.")

When I do, it throws an error in the chain midway saying
> Entering new AgentExecutor chain...
Traceback (most recent call last):
  File "/home/ramlah/Documents/projects/langchain-test/sql.py", line 96, in <module>
    agent_executor.run("list the tables in the db. Give the answer in a table json format.")
  File "/home/ramlah/Documents/projects/langchain/langchain/chains/base.py", line 236, in run
    return self(args[0], callbacks=callbacks)[self.output_keys[0]]
  File "/home/ramlah/Documents/projects/langchain/langchain/chains/base.py", line 140, in __call__
    raise e
  File "/home/ramlah/Documents/projects/langchain/langchain/chains/base.py", line 134, in __call__
    self._call(inputs, run_manager=run_manager)
  File "/home/ramlah/Documents/projects/langchain/langchain/agents/agent.py", line 953, in _call
    next_step_output = self._take_next_step(
  File "/home/ramlah/Documents/projects/langchain/langchain/agents/agent.py", line 773, in _take_next_step
    raise e
  File "/home/ramlah/Documents/projects/langchain/langchain/agents/agent.py", line 762, in _take_next_step
    output = self.agent.plan(
  File "/home/ramlah/Documents/projects/langchain/langchain/agents/agent.py", line 444, in plan
    return self.output_parser.parse(full_output)
  File "/home/ramlah/Documents/projects/langchain/langchain/agents/mrkl/output_parser.py", line 51, in parse
    raise OutputParserException(
langchain.schema.OutputParserException: Could not parse LLM output: `Action: list_tables_sql_db, ''`

Please help. Thanks!
Update
The recent updates to langchain version 0.0.215 seem to have fixed this issue, for me at least.
I am trying to ask questions against a multiple pdf using pinecone and openAI but I dont know how to.
The code below works for asking questions against one document. but I would like to have multiple documents to ask questions against:

# process_message.py
from flask import request
import pinecone
# from PyPDF2 import PdfReader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import ElasticVectorSearch, Pinecone, Weaviate, FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
import os
import json
# from constants.company import file_company_id_column, file_location_column, file_name_column
from services.files import FileFireStorage
from middleware.auth import check_authorization
import configparser
from langchain.document_loaders import UnstructuredPDFLoader, OnlinePDFLoader, PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter


def process_message():
    
    # Create a ConfigParser object and read the config.ini file
    config = configparser.ConfigParser()
    config.read('config.ini')
    # Retrieve the value of OPENAI_API_KEY
    openai_key = config.get('openai', 'OPENAI_API_KEY')
    pinecone_env_key = config.get('pinecone', 'PINECONE_ENVIRONMENT')
    pinecone_api_key = config.get('pinecone', 'PINECONE_API_KEY')


    loader = PyPDFLoader("docs/ops.pdf")
    data = loader.load()
    # data = body['data'][1]['name']
    # Print information about the loaded data
    print(f"You have {len(data)} document(s) in your data")
    print(f"There are {len(data[30].page_content)} characters in your document")

    # Chunk your data up into smaller documents
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0)
    texts = text_splitter.split_documents(data)
   

    embeddings = OpenAIEmbeddings(openai_api_key=openai_key)

    pinecone.init(api_key=pinecone_api_key, environment=pinecone_env_key)
    index_name = "pdf-chatbot"  # Put in the name of your Pinecone index here

    docsearch = Pinecone.from_texts([t.page_content for t in texts], embeddings, index_name=index_name)
    # Query those docs to get your answer back
    llm = OpenAI(temperature=0, openai_api_key=openai_key)
    chain = load_qa_chain(llm, chain_type="stuff")

    query = "Are there any other documents listed in this document?"
    docs = docsearch.similarity_search(query)
    answer = chain.run(input_documents=docs, question=query)
    print(answer)

    return answer

I added as many comments as I could there.
I got this information from https://www.youtube.com/watch?v=h0DHDp1FbmQ
I tried to look at other stackoverflow questions about this but could not find anything similar
finetuned a model (https://huggingface.co/decapoda-research/llama-7b-hf) using peft and lora and saved as https://huggingface.co/lucas0/empath-llama-7b. Now im getting Pipeline cannot infer suitable model classes from when trying to use it along with with langchain and chroma vectordb:
from langchain.embeddings import HuggingFaceHubEmbeddings
from langchain import PromptTemplate, HuggingFaceHub, LLMChain
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
from langchain.vectorstores import Chroma

repo_id = "sentence-transformers/all-mpnet-base-v2"
embedder = HuggingFaceHubEmbeddings(
    repo_id=repo_id,
    task="feature-extraction",
    huggingfacehub_api_token="XXXXX",
)
comments = ["foo", "bar"]
embeddings = embedder.embed_documents(texts=comments)
docsearch = Chroma.from_texts(comments, embedder).as_retriever()
#docsearch = Chroma.from_documents(texts, embeddings)

llm = HuggingFaceHub(repo_id='lucas0/empath-llama-7b', huggingfacehub_api_token='XXXXX')
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=docsearch, return_source_documents=False)

q = input("input your query:")
result = qa.run(query=q)

print(result["result"])


is anyone able to tell me how to fix this? Is it an issue with the model card? I was facing issues with the lack of the config.json file and ended up just placing the same config.json as the model I used as base for the lora fine-tuning. Could that be the origin of the issue? If so, how to generate the correct config.json without having to get the original llama weights?
Also, is there a way of loading several sentences into a custom HF model (not only OpenAi, as the tutorial show) without using vector dbs?
Thanks!

The same issue happens when trying to run the API on the model's HF page:
I am trying to understand GPT/langchain . I want to use my own data only but I am not able to find a basic example.
for example, I envision my chat to be something like this:
USER: show me way to build a tree house
GPT : To build a tree house you need the following materials and tools.....
MY owns data in a file mydata.txt  with the following content
To build a tree house you need the following tool hammer , nails and materials wood...
....
.....

Can you please show a simple example of how this can be done ..
I am using LangChain to create embeddings and then ask a question to those embeddings like so:
embeddings: OpenAIEmbeddings = OpenAIEmbeddings(disallowed_special=())
db = DeepLake(
    dataset_path=deeplake_url,
    read_only=True,
    embedding_function=embeddings,
)
retriever: VectorStoreRetriever = db.as_retriever()
model = ChatOpenAI(model_name="gpt-3.5-turbo") 
qa = ConversationalRetrievalChain.from_llm(model, retriever=retriever)
result = qa({"question": question, "chat_history": chat_history})

But I am getting the following error:
File "/xxxxx/openai/api_requestor.py", line 763, in _interpret_response_line
    raise self.handle_error_response(
openai.error.InvalidRequestError: This model's maximum context length is 4097 tokens. However, your messages resulted in 13918 tokens. Please reduce the length of the messages.

The chat_history is empty and the question is quite small.
How can I reduce the size of tokens being passed to OpenAI?
I'm assuming the response from the embeddings is too large being passed to openai. It might be easy enough to just figure out how to truncate the data being sent to openai.
I am trying to extract information about a csv using langchain and chatgpt.
If I just take a few lines of code and use the 'stuff' method it works perfectly. But when I use the whole csv with the map_reduce it fails in most of questions.
My current code is the following:
queries = ["Tell me the name of every driver who is German","how many german drivers are?",  "which driver uses the number 14?", "which driver has the oldest birthdate?"]

import os

from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv()) # read local .env file

from langchain.document_loaders import CSVLoader
from langchain.callbacks import get_openai_callback
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
from langchain.vectorstores import Chroma

files = ['drivers.csv','drivers_full.csv']

for file in files:
    print("=====================================")
    print(file)
    print("=====================================")
    with get_openai_callback() as cb:

        loader = CSVLoader(file_path=file,encoding='utf-8')
        docs = loader.load()

        from langchain.embeddings.openai import OpenAIEmbeddings

        embeddings = OpenAIEmbeddings()

        # create the vectorestore to use as the index
        db = Chroma.from_documents(docs, embeddings)
        # expose this index in a retriever interface
        retriever = db.as_retriever(search_type="similarity", search_kwargs={"k":1000, "score_threshold":"0.2"})

        for query in queries:
            qa_stuff = RetrievalQA.from_chain_type(
                llm=OpenAI(temperature=0,batch_size=20), 
                chain_type="map_reduce", 
                retriever=retriever,
                verbose=True
            )

            print(query)
            result = qa_stuff.run(query)

            print(result)
            
        print(cb)

If fails in answering how many german drivers are, driver with number 14, oldest birthdate. Also the cost is huge (8$!!!!)
You have the code here:
https://github.com/pablocastilla/langchain-embeddings/blob/main/langchain-embedding-full.ipynb
I'm using langchain with pinecode, it gives me 4 sourceDocs but I want only most relevant 1 sourceDoc.
I'm using javascript and don't know where to put top_k in code.
Here's my code
export const makeChain = (vectorStore) => {
  const model = new OpenAI({
    temperature: 0
    modelName: 'gpt-3.5-turbo', 
  });

  const chain = ConversationalRetrievalQAChain.fromLLM(
    model,
    vectorStore.asRetriever(),
    {
      qaTemplate: QA_PROMPT,
      questionGeneratorTemplate: CONDENSE_PROMPT,
      returnSourceDocuments: true,
    }
  );
  return chain;
};

/* create vectorStore*/
    const vectorStore = await PineconeStore.fromExistingIndex(
      new OpenAIEmbeddings({}),
      {
        pineconeIndex: index,
        textKey: 'text',
        namespace: PINECONE_NAME_SPACE, //namespace comes from your config folder
      }
    );

    //create chain
    const chain = makeChain(vectorStore);
    //Ask a question using chat history
    const response = await chain.call({
      question: sanitizedQuestion,
      chat_history: history || [],
    });
    ```
I am trying to put together a simple "Q&A with sources" using Langchain and a specific URL as the source data. The URL consists of a single page with quite a lot of information on it.
The problem is that RetrievalQAWithSourcesChain is only giving me the entire URL back as the source of the results, which is not very useful in this case.
Is there a way to get more detailed source info?
Perhaps the heading of the specific section on the page?
A clickable URL to the correct section of the page would be even more helpful!
I am slightly unsure whether the generating of the result source is a function of the language model, URL loader or simply RetrievalQAWithSourcesChain alone.
I have tried using UnstructuredURLLoader and SeleniumURLLoader with the hope that perhaps more detailed reading and input of the data would help - sadly not.
Relevant code excerpt:
llm = ChatOpenAI(temperature=0, model_name='gpt-3.5-turbo')
chain = RetrievalQAWithSourcesChain.from_llm(llm=llm, retriever=VectorStore.as_retriever())

result = chain({"question": question})

print(result['answer'])
print("\n Sources : ",result['sources'] )
I am a brand new user of Chroma database (and the associate python libraries).
When I call get on a collection, embeddings is always none, even if embeddings are explicitly set/defined when adding documents to a collection (so it can't be an issue with generating the embeddings - I don't think).
For the following code (Python 3.10, chromadb 0.3.26), I expected to see a list of embeddings in the returned dictionary, but it is none.
import chromadb

chroma_client = chromadb.Client()
collection = chroma_client.create_collection(name="my_collection")
collection.add(
    embeddings=[[1.2, 2.3, 4.5], [6.7, 8.2, 9.2]],
    documents=["This is a document", "This is another document"],
    metadatas=[{"source": "my_source"}, {"source": "my_source"}],
    ids=["id1", "id2"]
)

print(collection.get())

Output:
{'ids': ['id1', 'id2'], 'embeddings': None, 'documents': ['This is a document', 'This is another document'], 'metadatas': [{'source': 'my_source'}, {'source': 'my_source'}]}

The same issue does not occur when using query instead of get:
print(collection.query(query_embeddings=[[1.2, 2.3, 4.4]], include=["embeddings"]))

Output:
{'ids': [['id1', 'id2']], 'embeddings': [[[1.2, 2.3, 4.5], [6.7, 8.2, 9.2]]], 'documents': None, 'metadatas': None, 'distances': None}

The same issue occurs when using langchain wrappers.
Any ideas, friends? :-)
I'm trying to use langchain's pandas agent on python for some development work but it goes into a recursive loop due to it being unable to take action on a thought, the thought being, having to run some pandas code to continue the thought process for the asked prompt on some sales dataset (sales.csv).
here is the below code
import os
os.environ['OPENAI_API_KEY'] = 'sk-xxx'
from langchain.agents import create_pandas_dataframe_agent
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
import pandas as pd

df = pd.read_csv('sales.csv')
llm = ChatOpenAI(temperature=0.0,model_name='gpt-3.5-turbo')
pd_agent = create_pandas_dataframe_agent(llm, df, verbose=True)
pd_agent.run("what is the mean of the profit?")

and well the response it gives is as below  (i replaced ``` with ----)
> Entering new  chain...
Thought: We need to calculate the profit first by subtracting the cogs from the total, and then find the mean of the profit.
Action: Calculate the profit and find the mean using pandas.
Action Input:
----
df['Profit'] = df['Total'] - df['cogs']
df['Profit'].mean()
----
Observation: Calculate the profit and find the mean using pandas. is not a valid tool, try another one.
Thought:I need to use python_repl_ast to execute the code.
Action: Calculate the profit and find the mean using pandas.
Action Input: `python_repl_ast` 
----
df['Profit'] = df['Total'] - df['cogs']
df['Profit'].mean()
----
Observation: Calculate the profit and find the mean using pandas. is not a valid tool, try another one.
Thought:I need to use `python` instead of `python_repl_ast`.
Action: Calculate the profit and find the mean using pandas.
Action Input: `python`
----
import pandas as pd
df = pd.read_csv('filename.csv')
df['Profit'] = df['Total'] - df['cogs']
df['Profit'].mean()
----
.
.
.
.
.
.
Observation: Calculate the profit and find the mean using pandas. is not a valid tool, try another one.
Thought:

> Finished chain.


'Agent stopped due to iteration limit or time limit.'
Now my question is why is it not using the python_repl_ast tool to do the calculation?
I even changed this agent's tool's description (python_repl_ast ) which was
A Python shell. Use this to execute python commands. Input should be a valid python command. When using this tool, sometimes output is abbreviated - make sure it does not look abbreviated before using it in your answer.
into
A Python shell.   Use this to execute python commands and profit, mean calculation using pandas. Input should be a valid python command. When using this tool, sometimes output is abbreviated - make sure it does not look abbreviated before using it in your answer.
But it did not help. Also i noticed when the python_repl_ast is initialized into my agent the dataframe is loaded into it's local variables tools = [PythonAstREPLTool(locals={"df": df})] so I'm guessing I'm doing something wrong.
Any help will be greatly appreciated.
Thank you.
I am trying to create a chatbot with langchain and openAI that can query the database with large number of tables based on user query. I have used SQLDatabaseSequentialChain which is said to be best if you have large number of tables in the database.
The problem is when I run this code, it takes forever to establish the connection and at the end I get this error:
 raise self.handle_error_response(
openai.error.APIError: internal error {
        "message": "internal error",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}
 500 {'error': {'message': 'internal error', 'type': 'invalid_request_error', 'param': None, 'code': None}} {'Date': 'Wed, 21 Jun 2023 14:49:42 GMT', 'Content-Type': 
'application/json; charset=utf-8', 'Content-Length': '147', 'Connection': 'keep-alive', 'vary': 'Origin', 'x-request-id': '37d9d00a37ce69e68166317740bad7da', 'strict-transport-security': 'max-age=15724800; includeSubDomains', 'CF-Cache-Status': 'DYNAMIC', 'Server': 'cloudflare', 'CF-RAY': '7dad0f24fa9c6ec5-BOM', 'alt-svc': 'h3=":443"; ma=86400'}


Below is the code I found on the internet:
from langchain import OpenAI, SQLDatabase
from langchain.chains import SQLDatabaseSequentialChain
import pyodbc

server = 'XYZ'
database = 'XYZ'
username = 'XYZ'
password = 'XYZ'
driver = 'ODBC Driver 17 for SQL Server'

conn_str = f"mssql+pyodbc://{username}:{password}@{server}/{database}?driver={driver}"

try:
    # Establish a connection to the database
    conn = SQLDatabase.from_uri(conn_str)

except pyodbc.Error as e:
    # Handle any errors that occur during the connection or query execution
    print(f"Error connecting to Azure SQL Database: {str(e)}")

OPENAI_API_KEY = "XYZ key"

llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY, model_name='text-davinci-003 ')

PROMPT = """ 
Given an input question, first create a syntactically correct SQL query to run,  
then look at the results of the query and return the answer.  
The question: {question}
"""

db_chain = SQLDatabaseSequentialChain.from_llm(llm, conn, verbose=True, top_k=3)

question = "What is the property code of Ambassador, 821?"

db_chain.run(PROMPT.format(question=question))


I have confirmed that my openAI API key is up and running.
Please help me out with this.
Also if you have suggestions for any other method that I should consider, please let me know. I am currently doing RnD on this project but didn't found any satisfactory solution.
Thank you
I tried to check if my openAI API key is available and yes, it is. Expected to get a response from GPT model.
I'm just getting started with working with LLMs, particularly OpenAIs and other OSS models. There are a lot of guides on using LlamaIndex to create a store of all your documents and then query on them. I tried it out with a few sample documents, but discovered that each query gets super expensive quickly. I think I used a 50-page PDF document, and a summarization query cost me around 1.5USD per query. I see there's a lot of tokens being sent across, so I'm assuming it's sending the entire document for every query. Given that someone might want to use thousands of millions of records, I can't see how something like LlamaIndex can really be that useful in a cost-effective manner.
On the other hand, I see OpenAI allows you to train a ChatGPT model. Wouldn't that, or using other custom trained LLMs, be much cheaper and more effective to query over your own data? Why would I ever want to set up LlamaIndex?
I've searched all over langchain documentation on their official website but I didn't find how to create a langchain doc from a str variable in python so I searched in their GitHub code and I found this :
  doc=Document(
                page_content="text",
                metadata={"source": "local"}
            )


PS: I added the metadata attribute
then I tried using that doc with my chain:
Memory and Chain:
memory = ConversationBufferMemory(memory_key="chat_history", input_key="human_input")
chain = load_qa_chain(
    llm, chain_type="stuff", memory=memory, prompt=prompt
)


the call method:
  chain({"input_documents": doc, "human_input": query})

prompt template:
template = """You are a senior financial analyst analyzing the below document and having a conversation with a human.
{context}
{chat_history}
Human: {human_input}
senior financial analyst:"""

prompt = PromptTemplate(
    input_variables=["chat_history", "human_input", "context"], template=template
)

but I am  getting the following error:
AttributeError: 'tuple' object has no attribute 'page_content'


when I tried to check the type and the page content of the Document object before using it with the chain I got this
print(type(doc))
<class 'langchain.schema.Document'>
print(doc.page_content)
"text"