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


import streamlit as st
import os
import tempfile
from pathlib import Path
from pydantic import BaseModel, Field
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.agents import Tool
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.document_loaders import PyPDFLoader
from langchain.chains import RetrievalQA
from langchain.agents import initialize_agent
import openai
os.environ["OPENAI_API_KEY"] = ""
os.environ['OPENAI_API_TYPE'] = 'azure'
os.environ['OPENAI_API_VERSION'] = '2023-03-15-preview'
os.environ['OPENAI_API_BASE'] = "https://summarization"

#API settings for embedding
openai.api_type = "azure"
openai.api_base = "https://summarization"
openai.api_version = '2023-03-15-'
openai.api_key = ""

class DocumentInput(BaseModel):
    question: str = Field()

# Create a temporary directory in the script's folder
script_dir = Path(__file__).resolve().parent
temp_dir = os.path.join(script_dir, "tempDir")

def main():
    st.title("PDF Document Comparison")

    # Create a form to upload PDF files and enter a question
    st.write("Upload the first PDF file:")
    pdf1 = st.file_uploader("Choose a PDF file", type=["pdf"], key="pdf1")

    st.write("Upload the second PDF file:")
    pdf2 = st.file_uploader("Choose a PDF file", type=["pdf"], key="pdf2")

    question = st.text_input("Enter your question")
    submit_button = st.button("Compare PDFs")

    if submit_button:
        if pdf1 and pdf2:
            if not os.path.exists(temp_dir):
                os.makedirs(temp_dir)
            else:
                # Clear the previous contents of the "tempDir" folder
                for file in os.listdir(temp_dir):
                    file_path = os.path.join(temp_dir, file)
                    try:
                        if os.path.isfile(file_path):
                            os.unlink(file_path)
                    except Exception as e:
                        print(f"Error deleting file: {e}")

            # Save the PDF files to the "tempDir" directory
            pdf1_path = os.path.join(temp_dir, pdf1.name)
            with open(pdf1_path, 'wb') as f:
                f.write(pdf1.getbuffer())

            pdf2_path = os.path.join(temp_dir, pdf2.name)
            with open(pdf2_path, 'wb') as f:
                f.write(pdf2.getbuffer())

            llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo",engine="gpt-35-turbo")

            tools = []
            files = [

                {
                    "name": pdf1.name,
                    "path": pdf1_path,
                },

                {
                    "name": pdf2.name,
                    "path": pdf2_path,
                },
            ]

            for file in files:
                loader = PyPDFLoader(file["path"])
                pages = loader.load_and_split()
                text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
                docs = text_splitter.split_documents(pages)
                embeddings = OpenAIEmbeddings()
                retriever = FAISS.from_documents(docs, embeddings).as_retriever()

                # Wrap retrievers in a Tool
                tools.append(
                    Tool(
                        args_schema=DocumentInput,
                        name=file["name"],
                        description=f"useful when you want to answer questions about {file['name']}",
                        func=RetrievalQA.from_chain_type(llm=llm, retriever=retriever),
                    )
                )
            agent = initialize_agent(
                tools=tools,
                llm=llm,
                verbose=True,
            )

            st.write(agent({"input": question}))
            # Now you have both PDFs saved in the "tempDir" folder
            # You can perform your PDF comparison here

if __name__ == "__main__":
    main()

I get the following error :
pydantic.v1.error_wrappers.ValidationError: 1 validation error for Tool
args_schema
subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel) I am following the example from langchain documentation:https://python.langchain.com/docs/integrations/toolkits/document_comparison_toolkit


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)


I am trying to use a custom embedding model in Langchain with chromaDB. I can't seem to find a way to use the base embedding class without having to use some other provider (like OpenAIEmbeddings or HuggingFaceEmbeddings). Am I missing something?
On the Langchain page it says that the base Embeddings class in LangChain provides two methods: one for embedding documents and one for embedding a query. so I figured there must be a way to create another class on top of this class and overwrite/implement those methods with our own methods. But how do I do that?
I tried to somehow use the base embeddings class but am unable to create a new embedding object/class on top of it.


I Am trying to download a PDF file from a GCS storage bucket and read the content into memory.
When using Langchain with python, i can just use the GCSDirectoryLoader to read all the files in a bucket and the pdf text.
Langchain for NodeJs doesnt have GCSDirectoryLoader or a webloader for PDF files.
When downloading the file, i get a Document with the binary representation as content.
What is the best way to download pdf content from GCS bucket into memory?


I am retrieving the results from my internal Db but for this example I have added an open URL. I am using Azure openai and langchain in conjunction to build this retrieval engine. I checked in the Azure Portal that deployment is successful and i am able to run in a stand alone prompt.
The last query throws this error:

InvalidRequestError: Must provide an 'engine' or 'deployment_id' parameter to create a <class 'openai.api_resources.completion.Completion'>.

Since we can see I have already supplied a deployment_id above. What am I missing?
Here is the entire code
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.llms import OpenAI
from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader
from langchain.document_loaders import DirectoryLoader
loader= TextLoader(r'./RawText.txt', encoding='utf-8')
documents = loader.load()
text_splitter= RecursiveCharacterTextSplitter(chunk_size= 70, chunk_overlap=0)
texts= text_splitter.split_documents(documents)

#create the DB
    persist_directory = 'db'
#embedding= OpenAIEmbeddings()
   embeddings = OpenAIEmbeddings(deployment="text-embedding-ada-002",model="text-embedding-ada-002", chunk_size = 1)
   vectordb= Chroma.from_documents(documents=texts,
                               embedding=embeddings,
                               persist_directory=persist_directory)

Post this I am creating a retriever as below:
  vectordb= Chroma(persist_directory=persist_directory, embedding_function=embeddings)
    retriever= vectordb.as_retriever()
    docs=retriever.get_relevant_documents("Databricks")

#Creating a Chain:
 from langchain.chains import RetrievalQA
 import openai

#Specify the name of the engine you want to use
engine = "test_chat"
qa_chain=RetrievalQA.from_chain_type(llm=OpenAI(),
                                chain_type="stuff",
                                retriever= retriever,
                                return_source_documents=True)

#test_chat here for reference is text-embedding-ada-002
#Cite Source
def process_llm_responses(llm_response):
print(llm_response['result'])
print('\n\nSources:')
for source in llm_response["source_documents"]:
    print(source.metadata["source"])

#full retrieval in process
query = "What is A medallion architecture"
llm_response= qa_chain(query)
process_llm_responses


I'm trying to create an embedding vector database with some .txt documents in my local folder. In particular I'm following this tutorial from the official page of LangChain: LangChain - Azure Cognitive Search and Azure OpenAI.
I have followed all the steps of the tutorial and this is my Python script:
# From https://python.langchain.com/docs/integrations/vectorstores/azuresearch

import openai
import os
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores.azuresearch import AzureSearch

os.environ["OPENAI_API_TYPE"] = "azure"
os.environ["OPENAI_API_BASE"] = "https://xxxxxx.openai.azure.com"
os.environ["OPENAI_API_KEY"] = "xxxxxxxxx"
os.environ["OPENAI_API_VERSION"] = "2023-05-15"

model: str = "text-embedding-ada-002"

vector_store_address: str = "https://xxxxxxx.search.windows.net"
vector_store_password: str = "xxxxxxx"

embeddings: OpenAIEmbeddings = OpenAIEmbeddings(deployment=model, chunk_size=1)
index_name: str = "cognitive-search-openai-exercise-index"
vector_store: AzureSearch = AzureSearch(
    azure_search_endpoint=vector_store_address,
    azure_search_key=vector_store_password,
    index_name=index_name,
    embedding_function=embeddings.embed_query,
)

from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter

loader = TextLoader("C:/Users/xxxxxxxx/azure_openai_cognitive_search_exercise/data/qna/a.txt", encoding="utf-8")

documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

vector_store.add_documents(documents=docs)

# Perform a similarity search
docs = vector_store.similarity_search(
    query="Who is Pippo Franco?",
    k=3,
    search_type="similarity",
)
print(docs[0].page_content)

Now, when I run the script I get the following error:

vector_search_configuration is not a known attribute of class <class 'azure.search.documents.indexes.models._index.SearchField'> and will be ignored
algorithm_configurations is not a known attribute of class <class 'azure.search.documents.indexes._generated.models._models_py3.VectorSearch'> and will be ignored
Traceback (most recent call last):
  File "C:\Users\xxxxxxxxx\venv\Lib\site-packages\langchain\vectorstores\azuresearch.py", line 105, in _get_search_client
    index_client.get_index(name=index_name)
  File "C:\Users\xxxxxxx\venv\Lib\site-packages\azure\core\tracing\decorator.py", line 78, in wrapper_use_tracer
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxxxxxx\KYF\venv\Lib\site-packages\azure\search\documents\indexes\_search_index_client.py", line 145, in get_index
    result = self._client.indexes.get(name, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxxxxx\venv\Lib\site-packages\azure\core\tracing\decorator.py", line 78, in wrapper_use_tracer
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxxxxx\KYF\venv\Lib\site-packages\azure\search\documents\indexes\_generated\operations\_indexes_operations.py",
line 864, in get
    map_error(status_code=response.status_code, response=response, error_map=error_map)
  File "C:\Users\xxxxxxxx\venv\Lib\site-packages\azure\core\exceptions.py", line 165, in map_error
    raise error
azure.core.exceptions.ResourceNotFoundError: () No index with the name 'cognitive-search-openai-exercise-index' was found in the service 'cognitive-search-openai-exercise'.
Code:
Message: No index with the name 'cognitive-search-openai-exercise-index' was found in the service 'cognitive-search-openai-exercise'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\xxxxxxx\venv\azure_openai_cognitive_search_exercise\test.py", line 25, in <module>
    vector_store: AzureSearch = AzureSearch(
                                ^^^^^^^^^^^^
  File "C:\Users\xxxxxxx\venv\Lib\site-packages\langchain\vectorstores\azuresearch.py", line 237, in __init__
    self.client = _get_search_client(
                  ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxxxxxxx\venv\Lib\site-packages\langchain\vectorstores\azuresearch.py", line 172, in _get_search_client
    index_client.create_index(index)
  File "C:\Users\xxxxxxx\venv\Lib\site-packages\azure\core\tracing\decorator.py", line 78, in wrapper_use_tracer
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxxxxxx\venv\Lib\site-packages\azure\search\documents\indexes\_search_index_client.py", line 220, in create_index
    result = self._client.indexes.create(patched_index, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxxxxxx\venv\Lib\site-packages\azure\core\tracing\decorator.py", line 78, in wrapper_use_tracer
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxxxxx\venv\Lib\site-packages\azure\search\documents\indexes\_generated\operations\_indexes_operations.py",
line 402, in create
    raise HttpResponseError(response=response, model=error)
azure.core.exceptions.HttpResponseError: (InvalidRequestParameter) The request is invalid. Details: definition : The vector field 'content_vector' must have the property 'vectorSearchConfiguration' set.
Code: InvalidRequestParameter
Message: The request is invalid. Details: definition : The vector field 'content_vector' must have the property 'vectorSearchConfiguration' set.
Exception Details:      (InvalidField) The vector field 'content_vector' must have the property 'vectorSearchConfiguration' set. Parameters: definition
        Code: InvalidField
        Message: The vector field 'content_vector' must have the property 'vectorSearchConfiguration' set. Parameters: definition

I have created an index manually from the Azure Cognitive Search Console, but I don't think this is the correct approach, as the script should automatically create a new index.


I want to parallelize RetrievalQA with asyncio but I am unable to figure out how.
This is how my code works serially:
import langchain
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate, LLMChain
from langchain.chains import RetrievalQA
from langchain.vectorstores import FAISS
from langchain.schema.vectorstore import VectorStoreRetriever
import asyncio
import nest_asyncio

retriever = VectorStoreRetriever(vectorstore=FAISS(...))

chat = ChatOpenAI(model="gpt-3.5-turbo-16k", temperature=0.7)

qa_chain = RetrievalQA.from_llm(chat, retriever= retriever
                                                 #,memory=memory
                                                 , return_source_documents=True
                                                 )

queries = ['query1', 'query2', 'query3']
data_to_append = []

for query in queries :

    vectordbkwargs = {"search_distance": 0.9}
    result = qa_chain({"query": query, "vectordbkwargs": vectordbkwargs})

    data_to_append.append({"Query": query, "Source_Documents": result["source_documents"], "Generated_Text": result["result"]})

Here was my attempt to parallelize it with asyncio but RetrievalQA doesn't seem to work async:
import langchain
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate, LLMChain
from langchain.chains import RetrievalQA
from langchain.vectorstores import FAISS
from langchain.schema.vectorstore import VectorStoreRetriever
import asyncio
import nest_asyncio

retriever = VectorStoreRetriever(vectorstore=FAISS(...))

chat = ChatOpenAI(model="gpt-3.5-turbo-16k", temperature=0.7)

qa_chain = RetrievalQA.from_llm(chat, retriever= retriever
                                                 , return_source_documents=True
                                                 )

queries = ['query1', 'query2', 'query3']
data_to_append = []

async def process_query(query):

        vectordbkwargs = {"search_distance": 0.9}
        result = await qa_chain({"query": query, "vectordbkwargs": vectordbkwargs})
        data_to_append.append({"Query": query, "Source_Documents": result["source_documents"], "Generated_Text": result["result"]})

async def main():

    tasks = []

    for query in queries: # Iterate all rows
        task = process_query(query)
        tasks.append(task)

    await asyncio.gather(*tasks)

if __name__ == "__main__":
    nest_asyncio.apply()
    asyncio.run(main())

Any help would be greatly appreciated.


I am currently working on a project where I have implemented the ConversationalRetrievalQAChain, with the option "returnSourceDocuments" set to true. The system works perfectly when I ask specific questions related to the VectorStore database, as it returns matching sources. However, when I pose generic questions like "Is the earth round?" or "How are you today?" it returns unrelated sources that don't align with the query. It appears that the vector store always returns documents, even if they don't match the query.
I'm seeking guidance on how to enhance the relevance of the source documents retrieved by the Langchain ConversationalRetrievalQAChain. Are there specific tools or techniques within the Langchain framework that can help mitigate this behavior, or is it necessary to develop a manual process to assess document relevance? How can I effectively limit the retrieval of unrelated source documents in this scenario?
Here is relevant part of the code:
async init(): Promise<void> {
        try {
            this.driver = await this.getQdrantDriver()
            this.retriever = await this.createRetrieverFromDriver()
            this.chat = new ChatOpenAI({ modelName: aiConfig.modelName })
            this.chain = await this.createQAChain(this.chat)

            this.questionGenerationChain = await this.createQuestionGenerationChain()
            this.conversation = new ConversationalRetrievalQAChain({
                retriever: this.retriever,
                combineDocumentsChain: this.chain,
                questionGeneratorChain: this.questionGenerationChain,
                returnSourceDocuments: true,
            })
        } catch (error) {
            Logger.error(error.message)
            throw error
        }
    }

    private async createQuestionGenerationChain(): Promise<LLMChain> {
        const { default: Prompt } = await import('App/Models/Prompt')
        return new LLMChain({
            llm: this.chat,
            prompt: await Prompt.fetchCondensePrompt(),
        })
    }

    private async createRetrieverFromDriver(): Promise<VectorStoreRetriever<QdrantVectorStore>> {
        return this.driver.asRetriever(qdrantConfig.noResults ?? 5)
    }

    private async getQdrantDriver(embeddings = new OpenAIEmbeddings(), collectionName: string | null = null): Promise<QdrantVectorStore> {
        const { default: Ingest } = await import('App/Models/Ingest')
        return new QdrantVectorStore(
            embeddings,
            {
                url: qdrantConfig.qdrantUrl,
                collectionName: collectionName ?? await Ingest.lastCollection(),
            },
        )
    }

    private async createQAChain(chat: BaseLanguageModel<any, BaseLanguageModelCallOptions>): Promise<StuffDocumentsChain> {
        const { default: Prompt } = await import('App/Models/Prompt')
        return loadQAStuffChain(chat, {
            prompt: await Prompt.fetchQuestionPrompt(),
        })
    }


I'm attempted to pass draft documents and have my chatbot generate a template using a prompt create a non disclosure agreement draft for California between mike llc and fantasty world. with my code below the response i'm getting is:
"I'm sorry, but I cannot generate a non-disclosure agreement draft for you. However, you can use the provided context information as a template to create a non-disclosure agreement between Mike LLC and fantasty world. Just replace the placeholders in the template with the appropriate names and information for your specific agreement.
Here is my setup:
import sys
import os
import openai
import constants
import gradio as gr
from langchain.chat_models import ChatOpenAI

from llama_index import SimpleDirectoryReader, GPTListIndex, GPTVectorStoreIndex, LLMPredictor, PromptHelper, load_index_from_storage

# Disable SSL certificate verification (for debugging purposes)
os.environ['REQUESTS_CA_BUNDLE'] = ''  # Set it to an empty string

os.environ["OPENAI_API_KEY"] = constants.APIKEY
openai.api_key = os.getenv("OPENAI_API_KEY")
print(os.getenv("OPENAI_API_KEY"))

def createVecorIndex(path):
    max_input = 4096
    tokens = 512
    chunk_size = 600
    max_chunk_overlap = 0.1

    prompt_helper = PromptHelper(max_input, tokens, max_chunk_overlap, chunk_size_limit=chunk_size)

    #define llm
    llmPredictor = LLMPredictor(llm=ChatOpenAI(temperature=.7, model_name='gpt-3.5-turbo', max_tokens=tokens))

    #load data
    docs = SimpleDirectoryReader(path).load_data()

    #create vector index
    vectorIndex = GPTVectorStoreIndex(docs, llmpredictor=llmPredictor, prompt_helper=prompt_helper)
    vectorIndex.storage_context.persist(persist_dir='vectorIndex.json')

    return vectorIndex

vectorIndex = createVecorIndex('docs')

In my docs directory, I have a few examples of non-disclosure agreements to create the vector index.
This was my first attempt at the query:
def chatbot(input_index):
    query_engine = vectorIndex.as_query_engine()
    response = query_engine.query(input_index)
    return response.response

gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Super Awesome Chatbot").launch()

I can't seem to get it to generate the draft, it keeps giving me the "I cannot generate a draft" response
I also tried to create a clause for the word draft, but the setup below is essential useing the trained model instead my vector.
def chatbot(input_index):
    query_engine = vectorIndex.as_query_engine()

    # If the "draft" clause is active:
    if "draft" in input_index.lower():
        # Query the vectorIndex for relevant information/context
        vector_response = query_engine.query(input_index).response
        print(vector_response)
        # Use vector_response as context to query the OpenAI API for a draft
        prompt = f"Based on the information: '{vector_response}', generate a draft for the input: {input_index}"

        response = openai.Completion.create(
            engine="text-davinci-002",
            prompt=prompt,
            max_tokens=512,
            temperature=0.2
        )

        openai_response = response.choices[0].text.strip()

        return openai_response

    # If "draft" clause isn't active, use just the vectorIndex response
    else:
        print('else clause')
        return query_engine.query(input_index).response


This is how I am defining the executor
const executor = await initializeAgentExecutorWithOptions(tools, model, {
  agentType: 'chat-conversational-react-description',
  verbose: false,
});

Whenever I prompt the AI I have this statement at the end.
type SomeObject = {
  field1: number,
  field2: number,
}

- It is very critical that you answer only as the above object and JSON stringify it as a single string.
  Don't include any other verbose explanatiouns and don't include the markdown syntax anywhere.

The SomeObject is just an example. Usually it will have a proper object type.
When I use the executor to get a response from the AI, half the time I get the proper JSON string, but the other half the times are the AI completely ignoring my instructions and gives me a long verbose answer in just plain English...
How can I make sure I always get the structured data answer I want?
Maybe using the agentType: 'chat-conversational-react-description' isn't the right approach here?


I am using Langchain with OpenAI API for getting the summary of PDF Files. Some of my PDFs have many pages (more than the max token allowed in ChatGPT). Im trying two approaches to reduce the tokens so that I can input longer texts, but is still not working for a 300 inch- PDF.

Retrieval augmented generation: more specifically the text splitter

text_splitter = RecursiveCharacterTextSplitter(chunk_size = 1000, chunk_overlap = 50)
    all_splits = text_splitter.split_documents(data)

Text summarisation: using stuff documents chain

 stuff_chain = StuffDocumentsChain(llm_chain=llm_chain, document_variable_name="text")

I would like to understand what is the text splitter doing because is not helping me to input longer text in the prompt. How can do this?


I'm using langchain with Azure OpenAI and Azure Cognitive Search.
Currently I'm using Azure OpenAI text-embedding-ada-002 model for generating embeddings, but I would like to use a embbeding model from HugginFace if possible, because Azure OpenAI API does not allow to send documents in batches, so I need to make several calls and hit the rate limit.
I tried using this embbeding in my code:
embeddings = SentenceTransformerEmbeddings(
        model_name="all-mpnet-base-v2",
    )

Instead of:
embeddings = OpenAIEmbeddings(
        ...
)

The problem I'm facing, is that when I use  AzureSearch's aadd_texts method I get this error:
The vector field 'content_vector' dimensionality must match the field definition's 'dimensions' property. Expected: '1536'. Actual: '768'. (IndexDocumentsFieldError) 98: The vector field 'content_vector' dimensionality must match the field definition's 'dimensions' property. Expected: '1536'. Actual: '768'.
        Code: IndexDocumentsFieldError

I'm pretty lost. Did anyone used an open source embeddings model with Cognitive Search? How?


The following code do not do what it is supposed to do:
from langchain.callbacks.base import BaseCallbackHandler
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import VertexAI

class MyCustomHandler(BaseCallbackHandler):
    def on_llm_end(self, event, context):
        print(f"Prompt: {event.prompt}")
        print(f"Response: {event.response}")

llm = VertexAI(
            model_name='text-bison@001',
            max_output_tokens=1024,
            temperature=0.3,
            verbose=False)
prompt = PromptTemplate.from_template("1 + {number} = ")
handler = MyCustomHandler()
chain = LLMChain(llm=llm, prompt=prompt, callbacks=[handler])
response = chain.run(number=2)
print(response)

Based on this documentation and this tutorial, the code should execute the custom handler callback on_llm_end but in fact it doesn't.
Can anyone please tell me why?


I'm trying to test a chat agent using the python code below.  I'm using langchain agent and tool from langchain.  I'm defining a couple of simple functions for the LLM to use as tools when a prompt mentions something relevant to the tool.  I'm using the openai gpt-3.5-turbo model for the LLM.  I'm getting the error message below when trying to run conversational_agent with a simple prompt to return a random number.  The function defined for the tool should do this easily.  I'm getting the error message below mentioning that openai doesn't have the attribute.  Does anyone see what the issue might be and can you suggest how to fix it?
code:
from config import api_key,new_personal_api_key

apikey=new_personal_api_key

# apikey=api_key

import os

os.environ['OPENAI_API_KEY'] = apikey

from langchain.chains.conversation.memory import ConversationBufferWindowMemory

from langchain.agents import Tool
from langchain.tools import BaseTool

def meaning_of_life(input=""):
    return 'The meaning of life is 42 if rounded but is actually 42.17658'

life_tool = Tool(
    name='Meaning of Life',
    func= meaning_of_life,
    description="Useful for when you need to answer questions about the meaning of life. input should be MOL "
)

import random

def random_num(input=""):
    return random.randint(0,5)

random_tool = Tool(
    name='Random number',
    func= random_num,
    description="Useful for when you need to get a random number. input should be 'random'"
)

from langchain import OpenAI
from langchain.chat_models import ChatOpenAI

# Set up the turbo LLM
turbo_llm = ChatOpenAI(
    temperature=0,
    model_name='gpt-3.5-turbo'
)

from langchain.agents import initialize_agent

tools = [random_tool, life_tool]

# conversational agent memory
memory = ConversationBufferWindowMemory(
    memory_key='chat_history',
    k=3,
    return_messages=True
)

# create our agent
conversational_agent = initialize_agent(
    agent='chat-conversational-react-description',
    tools=tools,
    llm=turbo_llm,
#     llm=local_llm,
    verbose=True,
    max_iterations=3,
    early_stopping_method='generate',
    memory=memory,
    handle_parsing_errors=True
)

conversational_agent('Can you give me a random number?')

error:
> Entering new AgentExecutor chain...

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[12], line 1
----> 1 conversational_agent('Can you give me a random number?')

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chains/base.py:310, in Chain.__call__(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)
    308 except BaseException as e:
    309     run_manager.on_chain_error(e)
--> 310     raise e
    311 run_manager.on_chain_end(outputs)
    312 final_outputs: Dict[str, Any] = self.prep_outputs(
    313     inputs, outputs, return_only_outputs
    314 )

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chains/base.py:304, in Chain.__call__(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)
    297 run_manager = callback_manager.on_chain_start(
    298     dumpd(self),
    299     inputs,
    300     name=run_name,
    301 )
    302 try:
    303     outputs = (
--> 304         self._call(inputs, run_manager=run_manager)
    305         if new_arg_supported
    306         else self._call(inputs)
    307     )
    308 except BaseException as e:
    309     run_manager.on_chain_error(e)

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/agents/agent.py:1146, in AgentExecutor._call(self, inputs, run_manager)
   1144 # We now enter the agent loop (until it returns something).
   1145 while self._should_continue(iterations, time_elapsed):
-> 1146     next_step_output = self._take_next_step(
   1147         name_to_tool_map,
   1148         color_mapping,
   1149         inputs,
   1150         intermediate_steps,
   1151         run_manager=run_manager,
   1152     )
   1153     if isinstance(next_step_output, AgentFinish):
   1154         return self._return(
   1155             next_step_output, intermediate_steps, run_manager=run_manager
   1156         )

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/agents/agent.py:933, in AgentExecutor._take_next_step(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)
    930     intermediate_steps = self._prepare_intermediate_steps(intermediate_steps)
    932     # Call the LLM to see what to do.
--> 933     output = self.agent.plan(
    934         intermediate_steps,
    935         callbacks=run_manager.get_child() if run_manager else None,
    936         **inputs,
    937     )
    938 except OutputParserException as e:
    939     if isinstance(self.handle_parsing_errors, bool):

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/agents/agent.py:546, in Agent.plan(self, intermediate_steps, callbacks, **kwargs)
    534 """Given input, decided what to do.
    535
    536 Args:
   (...)
    543     Action specifying what tool to use.
    544 """
    545 full_inputs = self.get_full_inputs(intermediate_steps, **kwargs)
--> 546 full_output = self.llm_chain.predict(callbacks=callbacks, **full_inputs)
    547 return self.output_parser.parse(full_output)

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chains/llm.py:298, in LLMChain.predict(self, callbacks, **kwargs)
    283 def predict(self, callbacks: Callbacks = None, **kwargs: Any) -> str:
    284     """Format prompt with kwargs and pass to LLM.
    285
    286     Args:
   (...)
    296             completion = llm.predict(adjective="funny")
    297     """
--> 298     return self(kwargs, callbacks=callbacks)[self.output_key]

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chains/base.py:310, in Chain.__call__(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)
    308 except BaseException as e:
    309     run_manager.on_chain_error(e)
--> 310     raise e
    311 run_manager.on_chain_end(outputs)
    312 final_outputs: Dict[str, Any] = self.prep_outputs(
    313     inputs, outputs, return_only_outputs
    314 )

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chains/base.py:304, in Chain.__call__(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)
    297 run_manager = callback_manager.on_chain_start(
    298     dumpd(self),
    299     inputs,
    300     name=run_name,
    301 )
    302 try:
    303     outputs = (
--> 304         self._call(inputs, run_manager=run_manager)
    305         if new_arg_supported
    306         else self._call(inputs)
    307     )
    308 except BaseException as e:
    309     run_manager.on_chain_error(e)

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chains/llm.py:108, in LLMChain._call(self, inputs, run_manager)
    103 def _call(
    104     self,
    105     inputs: Dict[str, Any],
    106     run_manager: Optional[CallbackManagerForChainRun] = None,
    107 ) -> Dict[str, str]:
--> 108     response = self.generate([inputs], run_manager=run_manager)
    109     return self.create_outputs(response)[0]

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chains/llm.py:120, in LLMChain.generate(self, input_list, run_manager)
    118 callbacks = run_manager.get_child() if run_manager else None
    119 if isinstance(self.llm, BaseLanguageModel):
--> 120     return self.llm.generate_prompt(
    121         prompts,
    122         stop,
    123         callbacks=callbacks,
    124         **self.llm_kwargs,
    125     )
    126 else:
    127     results = self.llm.bind(stop=stop, **self.llm_kwargs).batch(
    128         cast(List, prompts), {"callbacks": callbacks}
    129     )

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chat_models/base.py:459, in BaseChatModel.generate_prompt(self, prompts, stop, callbacks, **kwargs)
    451 def generate_prompt(
    452     self,
    453     prompts: List[PromptValue],
   (...)
    456     **kwargs: Any,
    457 ) -> LLMResult:
    458     prompt_messages = [p.to_messages() for p in prompts]
--> 459     return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chat_models/base.py:349, in BaseChatModel.generate(self, messages, stop, callbacks, tags, metadata, run_name, **kwargs)
    347         if run_managers:
    348             run_managers[i].on_llm_error(e)
--> 349         raise e
    350 flattened_outputs = [
    351     LLMResult(generations=[res.generations], llm_output=res.llm_output)
    352     for res in results
    353 ]
    354 llm_output = self._combine_llm_outputs([res.llm_output for res in results])

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chat_models/base.py:339, in BaseChatModel.generate(self, messages, stop, callbacks, tags, metadata, run_name, **kwargs)
    336 for i, m in enumerate(messages):
    337     try:
    338         results.append(
--> 339             self._generate_with_cache(
    340                 m,
    341                 stop=stop,
    342                 run_manager=run_managers[i] if run_managers else None,
    343                 **kwargs,
    344             )
    345         )
    346     except BaseException as e:
    347         if run_managers:

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chat_models/base.py:492, in BaseChatModel._generate_with_cache(self, messages, stop, run_manager, **kwargs)
    488     raise ValueError(
    489         "Asked to cache, but no cache found at `langchain.cache`."
    490     )
    491 if new_arg_supported:
--> 492     return self._generate(
    493         messages, stop=stop, run_manager=run_manager, **kwargs
    494     )
    495 else:
    496     return self._generate(messages, stop=stop, **kwargs)

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chat_models/openai.py:365, in ChatOpenAI._generate(self, messages, stop, run_manager, stream, **kwargs)
    363 message_dicts, params = self._create_message_dicts(messages, stop)
    364 params = {**params, **kwargs}
--> 365 response = self.completion_with_retry(
    366     messages=message_dicts, run_manager=run_manager, **params
    367 )
    368 return self._create_chat_result(response)

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chat_models/openai.py:297, in ChatOpenAI.completion_with_retry(self, run_manager, **kwargs)
    293 def completion_with_retry(
    294     self, run_manager: Optional[CallbackManagerForLLMRun] = None, **kwargs: Any
    295 ) -> Any:
    296     """Use tenacity to retry the completion call."""
--> 297     retry_decorator = _create_retry_decorator(self, run_manager=run_manager)
    299     @retry_decorator
    300     def _completion_with_retry(**kwargs: Any) -> Any:
    301         return self.client.create(**kwargs)

File ~/anaconda3/envs/llm_110623/lib/python3.10/site-packages/langchain/chat_models/openai.py:77, in _create_retry_decorator(llm, run_manager)
     68 def _create_retry_decorator(
     69     llm: ChatOpenAI,
     70     run_manager: Optional[
     71         Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun]
     72     ] = None,
     73 ) -> Callable[[Any], Any]:
     74     import openai
     76     errors = [
---> 77         openai.error.Timeout,
     78         openai.error.APIError,
     79         openai.error.APIConnectionError,
     80         openai.error.RateLimitError,
     81         openai.error.ServiceUnavailableError,
     82     ]
     83     return create_base_retry_decorator(
     84         error_types=errors, max_retries=llm.max_retries, run_manager=run_manager
     85     )

AttributeError: module 'openai' has no attribute 'error'


I am extracting text from pdf documents and load it to Azure Cognitive Search for a RAG approach. Unfortunately this does not work. I am receiving the error message

AttributeError: 'str' object has no attribute 'page_content'

What I want to do is

Extract text from pdf via pymupdf - works
Upload it to Azuer Vector search as embeddings with vectors and `filename``
Query this through ChatGPT model

This is my code:
!pip install cohere tiktoken
!pip install openai==0.28.1
!pip install pymupdf
!pip install azure-storage-blob azure-identity
!pip install azure-search-documents --pre --upgrade
!pip install langchain

import fitz
import time
import uuid
import os
import openai

from PIL import Image
from io import BytesIO
from IPython.display import display

from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter

from langchain.chat_models import AzureChatOpenAI
from langchain.vectorstores import AzureSearch
from langchain.document_loaders import DirectoryLoader
from langchain.document_loaders import TextLoader
from langchain.text_splitter import TokenTextSplitter
from langchain.chains import ConversationalRetrievalChain
from langchain.prompts import PromptTemplate

from google.colab import drive

OPENAI_API_BASE = "https://xxx.openai.azure.com"
OPENAI_API_KEY = "xxx"
OPENAI_API_VERSION = "2023-05-15"

openai.api_type = "azure"
openai.api_key = OPENAI_API_KEY
openai.api_base = OPENAI_API_BASE
openai.api_version = OPENAI_API_VERSION

AZURE_COGNITIVE_SEARCH_SERVICE_NAME = "https://xxx.search.windows.net"
AZURE_COGNITIVE_SEARCH_API_KEY = "xxx"
AZURE_COGNITIVE_SEARCH_INDEX_NAME = "test"

llm = AzureChatOpenAI(deployment_name="gpt35", openai_api_key=OPENAI_API_KEY, openai_api_base=OPENAI_API_BASE, openai_api_version=OPENAI_API_VERSION)
embeddings = OpenAIEmbeddings(deployment_id="ada002", chunk_size=1, openai_api_key=OPENAI_API_KEY, openai_api_base=OPENAI_API_BASE, openai_api_version=OPENAI_API_VERSION)

acs = AzureSearch(azure_search_endpoint=AZURE_COGNITIVE_SEARCH_SERVICE_NAME,
                  azure_search_key = AZURE_COGNITIVE_SEARCH_API_KEY,
                  index_name = AZURE_COGNITIVE_SEARCH_INDEX_NAME,
                  embedding_function = embeddings.embed_query)

def generate_tokens(s):
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
  splits = text_splitter.split_text(s)

  return splits

drive.mount('/content/drive')
folder = "/content/drive/.../pdf/"

page_content = ''
doc_content = ''

for filename in os.listdir(folder):
    file_path = os.path.join(folder, filename)
    if os.path.isfile(file_path):
        print(f"Processing file: {file_path}")

        doc = fitz.open(file_path)
        for page in doc: # iterate the document pages
          page_content += page.get_text() # get plain text encoded as UTF-8
          doc_content += page_content

          d = generate_tokens(doc_content)

          # the following line throws the error
          # how can i add the chunks + filename to
          # Azure Cognitive Search?

          acs.add_documents(documents=d)

        print(metadatas)
        print("----------")
        print(doc_content)
        count = len(doc_content.split())
        print("Number of tokens: ", count)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-33-d9eaff7ee027> in <cell line: 10>()
     31           all_texts.extend(d)
     32
---> 33           acs.add_documents(documents=d)
     34
     35           metadatas = [{"Source": f"{i}-pl"} for i in range(len(all_texts))]

1 frames
/usr/local/lib/python3.10/dist-packages/langchain/schema/vectorstore.py in <listcomp>(.0)
    118         """
    119         # TODO: Handle the case where the user doesn't provide ids on the Collection
--> 120         texts = [doc.page_content for doc in documents]
    121         metadatas = [doc.metadata for doc in documents]
    122         return self.add_texts(texts, metadatas, **kwargs)

AttributeError: 'str' object has no attribute 'page_content'


I am extracting text from pdf documents and load it to Azure Cognitive Search for a RAG approach. Unfortunately this does not work. I am receiving the error message
HttpResponseError: () The request is invalid. Details: The property 'content' does not exist on type 'search.documentFields'. Make sure to only use property names that are defined by the type.
Code:
Message: The request is invalid. Details: The property 'content' does not exist on type 'search.documentFields'. Make sure to only use property names that are defined by the type.

What i want to do is

Extract text from pdf via pymupdf - works
Upload it to Azure Vector search as embeddings with vectors and metdata `filename``
Query this through ChatGPT model

Beside the error i want to add to this document object the metadata information filename but also dont know how to extend this ...
My code:
!pip install cohere tiktoken
!pip install openai==0.28.1
!pip install pymupdf
!pip install azure-storage-blob azure-identity
!pip install azure-search-documents --pre --upgrade
!pip install langchain

import fitz
import time
import uuid
import os
import openai

from PIL import Image
from io import BytesIO
from IPython.display import display

from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter

from langchain.chat_models import AzureChatOpenAI
from langchain.vectorstores import AzureSearch
from langchain.docstore.document import Document
from langchain.document_loaders import DirectoryLoader
from langchain.document_loaders import TextLoader
from langchain.text_splitter import TokenTextSplitter
from langchain.chains import ConversationalRetrievalChain
from langchain.prompts import PromptTemplate

from google.colab import drive

OPENAI_API_BASE = "https://xxx.openai.azure.com"
OPENAI_API_KEY = "xxx"
OPENAI_API_VERSION = "2023-05-15"

openai.api_type = "azure"
openai.api_key = OPENAI_API_KEY
openai.api_base = OPENAI_API_BASE
openai.api_version = OPENAI_API_VERSION

AZURE_COGNITIVE_SEARCH_SERVICE_NAME = "https://xxx.search.windows.net"
AZURE_COGNITIVE_SEARCH_API_KEY = "xxx"
AZURE_COGNITIVE_SEARCH_INDEX_NAME = "test"

llm = AzureChatOpenAI(deployment_name="gpt35", openai_api_key=OPENAI_API_KEY, openai_api_base=OPENAI_API_BASE, openai_api_version=OPENAI_API_VERSION)
embeddings = OpenAIEmbeddings(deployment_id="ada002", chunk_size=1, openai_api_key=OPENAI_API_KEY, openai_api_base=OPENAI_API_BASE, openai_api_version=OPENAI_API_VERSION)

acs = AzureSearch(azure_search_endpoint=AZURE_COGNITIVE_SEARCH_SERVICE_NAME,
                  azure_search_key = AZURE_COGNITIVE_SEARCH_API_KEY,
                  index_name = AZURE_COGNITIVE_SEARCH_INDEX_NAME,
                  embedding_function = embeddings.embed_query)

def generate_tokens(s, f):
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
  splits = text_splitter.split_text(s)
  i = 0

  documents = []
  for split in splits:
    metadata = {}
    metadata["index"] = i
    metadata["file_source"] = f
    i = i+1

    new_doc = Document(page_content=split, metadata=metadata)
    documents.append(new_doc)
    #documents = text_splitter.create_documents(splits)

  print (documents)

  return documents

drive.mount('/content/drive')
folder = "/content/drive/.../pdf/"

page_content = ''
doc_content = ''

for filename in os.listdir(folder):
    file_path = os.path.join(folder, filename)
    if os.path.isfile(file_path):
        print(f"Processing file: {file_path}")

        doc = fitz.open(file_path)
        for page in doc: # iterate the document pages
          page_content += page.get_text() # get plain text encoded as UTF-8
          d = generate_tokens(doc_content)

          # the following line throws the error
          # how can i add the chunks + filename to
          # Azure Cognitive Search?

          doc_content += page_content
          d = generate_tokens(doc_content, file_path)

          acs.add_documents(documents=d)

        print(metadatas)
        print("----------")
        print(doc_content)
        count = len(doc_content.split())
        print("Number of tokens: ", count)

HttpResponseError                         Traceback (most recent call last)
<ipython-input-11-d9eaff7ee027> in <cell line: 10>()
     31           all_texts.extend(d)
     32
---> 33           acs.add_documents(documents=d)
     34
     35           metadatas = [{"Source": f"{i}-pl"} for i in range(len(all_texts))]

7 frames
/usr/local/lib/python3.10/dist-packages/azure/search/documents/_generated/operations/_documents_operations.py in index(self, batch, request_options, **kwargs)
   1249             map_error(status_code=response.status_code, response=response, error_map=error_map)
   1250             error = self._deserialize.failsafe_deserialize(_models.SearchError, pipeline_response)
-> 1251             raise HttpResponseError(response=response, model=error)
   1252
   1253         if response.status_code == 200:

HttpResponseError: () The request is invalid. Details: The property 'content' does not exist on type 'search.documentFields'. Make sure to only use property names that are defined by the type.
Code:
Message: The request is invalid. Details: The property 'content' does not exist on type 'search.documentFields'. Make sure to only use property names that are defined by the type.

This is my index in Azure Cognitive Search index:


I'm working on an AI project but my current problem right now is that FAISS is taking far too long to load the documents. So Iv moved it into its own service via fastapi.
Everything Looks ok, but when I run it I get the error of:
id not find openai_api_key, please add an environment variable `OPENAI_API_KEY`

In my code:
embeddings = OpenAIEmbeddings()
db = FAISS.from_documents(documents, embeddings)

Now I am Using OpenAI but not in this service so i did not add my key.
From my understanding its just taking text tokenizing it using openAI's token map, and then doing a search and finding the nearest related documents based on that query.
That, Technically does not actually reach out to Open AI servers does it?
Afterwords i'm just adding the related documents to the prompt that I Send to Open AI's servers, So if its sending data to open AI twice that a tad inefficient right?
How can I get this to just be its own service? Or am I wasting my time here?


I'm trying to build a simple RAG, and I'm stuck at this code:
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from llama_index import LangchainEmbedding, ServiceContext

embed_model = LangchainEmbedding(
  HuggingFaceEmbeddings(model_name="thenlper/gte-large")
)
service_context = ServiceContext.from_defaults(
    chunk_size=256,
    llm=llm,
    embed_model=embed_model
)
index = VectorStoreIndex.from_documents(documents, service_context=service_context)

where I get ImportError: cannot import name 'LangchainEmbedding' from 'llama_index'
How can I solve? Is it related to the fact that I'm working on Colab?


I have deployed llm model locally which follows openai api schema. As it's endpoint follows openai schema, I don't want to write separate inference client.
Is there any way we can utilize existing openai wrapper by langchain to do inference for my localhost model.
I checked there is a openai adapter by langchain, but it seems like it require provider, which again I have to write separate client.
Overall goal it to not write any redundant code as it's already been maintained by langchain and may change with time. We can modify our api wrt openai and it works out of the box.
Your suggestion is appreciated.


Trying to conenct postgresql with langchain.llm used - AzureOpenAI
from langchain.llms import AzureOpenAI

llms = AzureOpenAI( temperature=0,deployment_name="gpt3turbo".......)

toolkit = SQLDatabaseToolkit(db=db,llm=llms)

Error:
ValidationError: 1 validation error for SQLDatabaseToolkit
llm
  value is not a valid dict (type=type_error.dict)

Tried different versions of langchain


I am new to langchain and following a tutorial code as below
from langchain.vectorstores import Chroma
from langchain.embeddings.openai import OpenAIEmbeddings
persist_directory = "C:/Users/shang/Documents/test/"
embedding = OpenAIEmbeddings()
vectordb = Chroma(persist_directory, embedding_function=embedding)

it kept prompt error. Did I miss anything here? Thanks
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
g:\My Drive\DataScience\LLM\LongChain\all_inclusive.ipynb Cell 5 line 5
      3 persist_directory = 'C:/Users/shang/Documents/test/'
      4 embedding = OpenAIEmbeddings()
----> 5 vectordb = Chroma(persist_directory, embedding_function=embedding)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain\vectorstores\chroma.py:81, in Chroma.__init__(self, collection_name, embedding_function, persist_directory, client_settings, collection_metadata, client, relevance_score_fn)
     79 """Initialize with a Chroma client."""
     80 try:
---> 81     import chromadb
     82     import chromadb.config
     83 except ImportError:

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\chromadb\__init__.py:3
      1 from typing import Dict, Optional
      2 import logging
----> 3 from chromadb.api.client import Client as ClientCreator
      4 from chromadb.api.client import AdminClient as AdminClientCreator
      5 import chromadb.config

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\chromadb\api\client.py:31
     27 from chromadb.types import Database, Tenant, Where, WhereDocument
     28 import chromadb.utils.embedding_functions as ef
---> 31 class SharedSystemClient:
...
   1011     os.stat() does.
   1012     """
-> 1013     return os.stat(self, follow_symlinks=follow_symlinks)

OSError: [WinError 433] A device which does not exist was specified: '.env'


I am using Llama2[7b model]-hugging face and lang-chain to do a simple address segregation/classification task. I want the model to find the city, state and country from the input string.I want my answer/query formatted in a particular way for a question-answering/ text-generation task.I understand that i can use FewShotPromptTemplate, where in i can show some examples to the LLM and get the output in the format i want.
I generated a few examples to feed in as samples :
examples = [
    {"input": "Plot No. 7, Sector 22, Noida, Uttar Pradesh, 201301, India",
     "Address": "Plot No. 7, Sector 22, Noida",
     "City" : "Noida",
     "State" : "Uttar Pradesh",
     "Country" : "India"},

    {"input": "Banjara Hills, Telangana, 500034, India",
     "Address": "Banjara Hills",
     "City" : "Not present",
     "State" : "Telangana",
     "Country" : "India"},

]

I set the template
example_formatter_template = """
input: {input},
Address : {Address},
City : {Address},
State : {State},
Country : {Country},
         \n
"""
# prompt
example_prompt = PromptTemplate(
    input_variables=["input", "Address", "City", "State", "Country"],
    template=example_formatter_template)

few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="What is the address, city, state, country in the string : ",
    suffix="input: {input}\n ",
    input_variables=["input"],
    example_separator="\n")

chain = LLMChain(llm=llm, prompt=few_shot_prompt, verbose = True)

# Run the chain only specifying the input variable.
print(chain.run("B-12, Gandhi Colony, Bhopal, Madhya Pradesh, 462016, India"))

Here is an example of what i want :
    {"input": "B-12, Gandhi Colony, Bhopal, Madhya Pradesh, 462016, India",

     "Address": "B-12, Gandhi Colony",
     "City" : "Bhopal",
     "State" : "Madhya Pradesh",
     "Country" : "India"},

I keep getting : format the expected output correctly from the model. And nothing is hence returned.
Additionally, I want to prevent the model from adding any extra information which
is not present in the context/string otherwise the queries take very long to respond.
i.e return '' or not found if city or state or country is not present in sting.
can someone help ?


Here is a simple code to use Redis and embeddings but It's not clear how can I build and load own embeddings and then pull it from Redis and use in search
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores.redis import Redis

embeddings = OpenAIEmbeddings
metadata = [
    {
        "user": "john",
        "age": 18,
        "job": "engineer",
        "credit_score": "high"
    }
]
texts = ["foo", "foo", "foo", "bar", "bar"]

rds = Redis.from_texts(
    texts,
    embeddings,
    metadata,
    redis_url="redis://localhost:6379",
    index_name="users",
)

results = rds.similarity_search("foo")
print(results[0].page_content)

But I want to load a text from e.g. text file, create embedings and load into Redis for later use. Something like this:
from openai import OpenAI
client = OpenAI()

def get_embedding(text, model="text-embedding-ada-002"):
    text = text.replace("\n", " ")
    return client.embeddings.create(input = [text], model=model).data[0].embedding

Does anyone have good example to implement this approach? Also wondering about TTL for embedings in Redis


langchain python agent react differently, for one prompt, it can import scanpy library, but not for the other one. My question is how to make sure to import the correct library without problem.
from dotenv import load_dotenv, find_dotenv
import openai
import os
from langchain.chat_models import ChatOpenAI
from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits import create_python_agent
from langchain_experimental.tools import PythonREPLTool
import scanpy as sc

load_dotenv(find_dotenv())
openai.api_key = os.environ["OPENAI_API_KEY"]

agent_executor = create_python_agent(
    llm=ChatOpenAI(temperature=0, model="gpt-4-1106-preview"),
    tool=PythonREPLTool(),
    verbose=True,
    agent_type=AgentType.OPENAI_FUNCTIONS,
    agent_executor_kwargs={"handle_parsing_errors": True},
)

if run the following,
agent_executor.run("set scanpy setting verbosity = 3 ")
I get
> Entering new AgentExecutor chain...

Invoking: Python_REPL with import scanpy as sc
sc.settings.verbosity = 3
print(sc.settings.verbosity)

3
The verbosity level of Scanpy has been set to 3.

> Finished chain.
The verbosity level of Scanpy has been set to 3.

but, if run the following,
pbmc = sc.datasets.pbmc68k_reduced()
agent_executor.run("use 'scanpy' library and 'pbmc' object to plot a umap")

I get,
> Entering new AgentExecutor chain...
Python REPL can execute arbitrary code. Use with caution.

Invoking: Python_REPL with import scanpy as sc

Invoking: Python_REPL with import scanpy as sc
responded: It seems there was an issue with the execution of the import statement for the 'scanpy' library. I will attempt to resolve this and proceed with the task. Let's try importing the library again.

It appears that there is an issue with importing the 'scanpy' library in this environment. Without being able to import the library, I cannot proceed with plotting a UMAP of the 'pbmc' object. If the library and the necessary data were available, I would typically load the data, preprocess it, and then use the sc.pl.umap function to plot the UMAP. However, since I cannot execute the code here, I'm unable to complete this task.


My code uses "wikipedia" to search for the relevant content. Below is the code
Load tools
tools = load_tools(
    ["wikipedia"],
    llm=llm)
agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    handle_parsing_errors=True,
    verbose=False
)
out = agent(f"Does {var_1} cause {var_2} or the other way around?.")

Instead of "wikipedia", I want to use my own pdf document that is available in my local. Can anyone help me in doing this?
I have tried using the below code
from langchain.document_loaders import PyPDFium2Loader
loader = PyPDFium2Loader("hunter-350-dual-channel.pdf")
data = loader.load()

but i am not sure how to include this in the agent.


After installing pip install langchain-experimental I have tried:
from langchain_experimental.sql_database import SQLDatabase

But it does not work. The code is as follows:
# 1. Load db with  langchain
from langchain.sql_database import SQLDatabase
db = SQLDatabase.from_uri("sqlite:////python/chatopenai/ecommerce.db")

# 2. Import APIs
import a_env_vars
import os
os.environ["OPENAI_API_KEY"] = a_env_vars.OPENAI_API_KEY

# 3. Create LLM
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(temperature=0,model_name='gpt-3.5-turbo')

# 4. Create chain
from langchain import SQLDatabaseChain
cadena = SQLDatabaseChain(llm = llm, database = db, verbose=False)

And the error is:
ImportError: cannot import name 'SQLDatabaseChain' from 'langchain' (C:\Users\jcarr\AppData\Local\Programs\Python\Python311\Lib\site-packages\langchain\__init__.py) Traceback: File "C:\Users\jcarr\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 534, in _run_script
    exec(code, module.__dict__) File "C:\python\chatOpenAI\c_front_end.py", line 3, in <module>
    import b_backend File "C:\python\chatOpenAI\b_backend.py", line 15, in <module>
    from langchain import SQLDatabaseChain

This is after doing the same with "langchain.sql_database".


LangChain's BaseMessage has a function toJSON that returns a Serialized.
Once I have a list of BaseMessages, I can use toJSON to serialize them, but how can I later deserialize them?
const messages = [
  new HumanMessage("hello"),
  new AIMessage("foo"),
  new HumanMessage("bar"),
  new AIMessage("baz"),
];

const serialized = messages.map((message) => message.toJSON());

const deserialized = ???


I am using the llama2 quantized model from Huggingface and loading it using ctransformers from langchain. When I run the query, I got the below warning
Number of tokens (512) exceeded maximum context length (512)
Below is my code:
from langchain.llms import CTransformers
llm = CTransformers(model='models_k/llama-2-7b-chat.ggmlv3.q2_K.bin',
                      model_type='llama',
                      config={'max_new_tokens': 512,
                              'temperature': 0.01}
                      )

B_INST, E_INST = "[INST]", "[/INST]"
B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"

DEFAULT_SYSTEM_PROMPT="""\
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible.
Please ensure that your responses are socially unbiased and positive in nature.

If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct.
If you don't know the answer to a question, please don't share false information."""

instruction = db_schema + " Based on the database schema provided to you \n Convert the following text from natural language to sql query: \n\n {text} \n only display the sql query"

SYSTEM_PROMPT = B_SYS + DEFAULT_SYSTEM_PROMPT + E_SYS

template = B_INST + SYSTEM_PROMPT + instruction + E_INST

prompt = PromptTemplate(template=template, input_variables=["text"])
LLM_Chain=LLMChain(prompt=prompt, llm=llm)
print(LLM_Chain.run("List the names and prices of electronic products that cost less than $500."))

Can anyone tell me why am i getting this error? Do I have to change the settings?


I'm using Langchain 0.0.345. I cannot get a verbose output of what's going on under the hood using the LCEL approach to chain building.
I have this code:
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser
from langchain.globals import set_verbose

set_verbose(True)

prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
model = ChatOpenAI()
output_parser = StrOutputParser()

chain = prompt | model | output_parser

chain.invoke({"topic": "ice cream"})

According to the documentation using set_verbose is the way to have a verbose output showing intermediate steps, prompt builds etc. But the output of this script is just a string without any intermediate steps.
Actually, the module langchain.globals does not appear even mentioned in the API documentation.
I have also tried setting the verbose=True parameter in the model creation, but it also does not work. This used to work with the former approach building with classes and so.
How is the recommended and current approach to have the output logged so you can understand what's going on?
Thanks!


This piece of code seems to not work. Even though this is the way that Pinecone have stated in their documentation that it should look like.
vectorstore = Pinecone(index, embeddings.embed_query, text_field)
The error/warning is
C:\Users\ndira\casetext-test-server\Lib\site-packages\langchain\vectorstores\pinecone.py:59: UserWarning: Passing in "embedding" as a Callable is deprecated. Please pass in an Embeddings object instead.   warnings.warn(
I don't know any other way of solving this. Kindly help thanks.


I loaded pdf files from a directory and I need to split them to smaller chunks to make a summary. The problem is that I can't iterate on documents object in a for loop and I get an error like this: AttributeError: 'tuple' object has no attribute 'page_content'
How can I iterate on my document items to call the summary function for each of them?
Here is my code:
# Load the documents

from langchain.document_loaders import DirectoryLoader
document_directory = "pdf_files"
loader = DirectoryLoader(document_directory)
documents = loader.load()

text_splitter = RecursiveCharacterTextSplitter(chunk_size=4000, chunk_overlap=50)

# Iterate on long pdf documents to make chunks (2 pdf files here)
for doc in documents:

  # it fails on this line
  texts = text_splitter.split_documents(doc)
  chain = load_summarize_chain(llm, chain_type="map_reduce", map_prompt=prompt, combine_prompt=prompt)


I have an LLM Chat model with token limitation.
I am trying to pass Sample User Messages and Expected AI Message Responses to the LLM to train it how to provide a response based on text extracted from a document.
I am loading the document with System Loader
 Document document = loadDocument(toPath("file:///filepath\\filename.pdf"));

I am using regex splitter to help the LLM understand a pattern
   DocumentByRegexSplitter splitter=new DocumentByRegexSplitter(regex,joiner,maxCharLimit,maxOverlap,subSplitter);

After embedding the document (In-Memory embedding store and getting the relevant vectors), I join it into an information string which I can feed into a prompt template to generate a User Message
PromptTemplate promptTemplate = PromptTemplate.from(
            "Answer the following question to the best of your ability"
                    + "Question:\n"
                    + "{{question}}\n"
                    + "\n"
                    + "Base your answer on the following information:\n"
                    + "{{information}}");

String information = relevantEmbeddings.stream()
        .map(match -> match.embedded().text())
        .collect(joining("\n\n"));

Map<String, Object> variables = new HashMap<>();
variables.put("question", trainingQuestion);
variables.put("information", information);
Prompt prompt = promptTemplate.apply(variables);

List<ChatMessage> chatMessages=new ArrayList<>();
chatMessages.add(prompt .toUserMessage());
chatMessages.add(new AiMessage("Expected Response"));

    variables.put("question", actualQuestion);
    variables.put("information", information);
    prompt = promptTemplate.apply(variables);
chatMessages.add(prompt .toUserMessage());

I will add the traning messages to a List as required by the Java Langchain framework
AiMessage response=chatModel.generate(chatMessages);

To make a long story short, I am facing the token constraint because of embedding the same document information for all the Few Shot messages.
Is there a way to make the LLM use the same document as a reference for the Few-Shot training and the actual query so I can avoid consuming tokens for the document multiple times?


I was getting an error when trying to use a Pydantic schema as an args_schema parameter value on a @tool decorator, following the DeepLearning.AI course.
My code was:
from pydantic import BaseModel, Field

class SearchInput(BaseModel):
    query: str = Field(description="Thing to search for")

@tool(args_schema=SearchInput)
def search(query: str) -> str:
    """Searches for weather online"""
    return "21c"

And was getting this error:
ValidationError: 1 validation error for StructuredTool
args_schema subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel)


I ran the bellow code. However, currently, it shows an error massage.
from langchain.llms import GooglePalm

api_key = 'my_API'

llm = GooglePalm(google_api_key=api_key,
                 temperature=0.1)

This is the error I got.
NotImplementedError                       Traceback (most recent call last)
<ipython-input-2-a3e32679669c> in <cell line: 7>()
      5
      6 # Create llm variable here
----> 7 llm = GooglePalm(google_api_key=api_key,
      8                  temperature=0.1)

2 frames
/usr/local/lib/python3.10/dist-packages/langchain_core/_api/deprecation.py in warn_deprecated(since, message, name, alternative, pending, obj_type, addendum, removal)
    293         if not removal:
    294             removal = f"in {removal}" if removal else "within ?? minor releases"
--> 295             raise NotImplementedError(
    296                 f"Need to determine which default deprecation schedule to use. "
    297                 f"{removal}"

NotImplementedError: Need to determine which default deprecation schedule to use. within ?? minor releases

Can someone please help me to solve this?
I need to create the large language model variable.


I am generating chromba db which has vector embeddings for pdf different documents and I want to store them to avoid re computation every time for different inputs. Pickling and Json serialization does not seem to work for chroma object, importing from another file also makes the embedding script run again.


I'm currently working on a project that involves Language Models (LLMs) and Chat Models, and I'm using the langchain library in Python to list available models. However, I'm encountering an ImportError when running the code.
Here's the code snippet I'm using:
from langchain.chat_models import list_available_models
model_names = list_available_models()
print(model_names)

The error message I receive is as follows:
ImportError: cannot import name 'list_available_models' from 'langchain.chat_models' (c:\Users\Edge\AppData\Local\Programs\Python\Python39\lib\site-packages\langchain\chat_models\__init__.py)

I've double-checked the library and the code, but I can't seem to find a solution to this issue. Could someone please help me understand what might be causing this ImportError and how I can resolve it?


i'm trying to create a chatbot using OpenAi Langchain and a cloud database (MongoDb in my case). What I do, is load a PDF, I read the data, create chunks from it and then create embeddings using "text-embedding-ada-002" by OpenAi. After that I store in my DB the filename, the text of the PDF the list of embeddings, and the list of messages. It works good, but the problem is that i want to load the list of embeddings to create the Conversation Chain, but i don't know if it is possible to create it from the list of embeddings of i should save another thing and not the list of embeddings, because i don't want to create them each time i open the chat of the current PDf
If i use something like this to generate the vector store and then run the below code to create the conversation chain it works, but i want to load the list of embeddings i saved in the db
def get_embeddings(chunks: list[str]):
    embeddings = OpenAIEmbeddings()
    vector_store = MongoDBAtlasVectorSearch.from_texts(
        texts=chunks,
        embedding=embeddings,
        collection=embeddings_collection,
        index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,
    )
    return vector_store

def get_conversation_chain(vector_store):
    memory = ConversationBufferMemory(
        memory_key="chat_history",
        vector_store=vector_store,
        similarity_threshold=0.8,
        max_memory_size=100,
        return_messages=True,
        input_key="question")
    conversation_chain = ConversationalRetrievalChain.from_llm(
        retriever=vector_store.as_retriever(),
        llm=llm,
        memory=memory)
    result = conversation_chain({"question": "what is the text about"})
    print(result)
    return conversation_chain

Is there a way to create a vector_store from the list of embeddings i saved? or should i use another type of conversation chain?


I am using Langchain to connect to OpenAi and some basic python calculation. below is the code i am using:
from langchain.llms.fake import FakeListLLM
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
tools = load_tools(["python_repl"])
responses=["Action: Python REPL\nAction Input: print(2 + 2)",
"Final Answer: 4"
]
llm = FakeListLLM(responses=responses)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True)
agent.run("whats 2 + 2").

I am referred to the langchain document and the code seems to be fine, there is no sysntexical error. in another code i called another library and created a new object here as:
from langchain_experimental.utilities import PythonREPL
python_repl = PythonREPL()

This code when ran on a simple instance runs:
example:
python_repl.run("print(10+34)")

But when i try to call python_repl from load_tool it throws error as ValueError: Got unknown tool python_repl. what is missed in the above code block.


I'm trying to setup a local chatbot demo for testing purpose. I wanted to use LangChain as the framework and LLAMA as the model. Tutorials I found all involve some registration, API key, HuggingFace, etc, which seems unnecessary for my purpose.
Is there a way to use a local LLAMA comaptible model file just for testing purpose? And also an example code to use the model with LangChain would be appreciated. Thanks!
UPDATE: I wrote a blog post based on the accepted answer.