mteb/FreshStackRetrieval huggingface dataset:
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 = "
", "
"
B_SYS, E_SYS = "
\n", "\n
\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.
Most samples of using LangChain's Expression Language (LCEL) look like this:
chain = setup_and_retrieval | prompt | model | output_parser

How can I access the source_documents in a RAG application when using this expression language?
Following LangChain docs in my Jupyter notebook with the following code :
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser


prompt = ChatPromptTemplate.from_template("Tell me a short joke about {topic}")
model = ChatOpenAI(model="gpt-3.5-turbo")
output_parser = StrOutputParser()

chain = prompt | model | output_parser

Docs say that pip install langchain installs all necessary modules, including langchain-community and langchain-core
However, I get this error:
ModuleNotFoundError: No module named 'langchain_openai'
I'm having trouble using LangChain embedding with Azure OpenAI credentials - it's showing a 404 error for resource not found.
stack trace: Error: 404 Resource not found
    at APIError.generate (c:\abcproject\node_modules\openai\error.js:53:20

import { OpenAIEmbeddings } from "@langchain/openai"

export const embeddingModel = new OpenAIEmbeddings({ 
  azureOpenAIApiKey: "AzureOpenAI api key",
  azureOpenAIApiVersion: "2023-08-01-preview",
  azureOpenAIApiDeploymentName: "gpt-4-32k",
  azureOpenAIBasePath:"Azure OpenAI endpoint"
});
When I run the RAG chain code with OpenAI from langchain it gives me warning like this:
PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/
  warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', DeprecationWarning)

I have not place to replace dict with model_dump and I even have not encode it anywhere in my code. Any idea how to solve this warning?
Here is my code:
from client_setup import get_client
#from langchain_community.vectorstores import Weaviate
#from langchain_openai.OpenAI import OpenAI
from langchain_openai import OpenAI
from langchain_community.vectorstores import Weaviate
from langchain.retrievers.weaviate_hybrid_search import WeaviateHybridSearchRetriever
from langchain.prompts import ChatPromptTemplate
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.output_parser import StrOutputParser


client = get_client()

retriever = WeaviateHybridSearchRetriever(
        client=client, 
        index_name="Material",
        text_key="su",
        attributes=["material", "heat_treatment", "su", "sy"],
        create_schema_if_missing=True,
        )

llm = OpenAI(api_key="", model_name="gpt-3.5-turbo-instruct")


template = """You are an assistant for question-answering tasks. 
Use the following pieces of retrieved context to answer the question. 
If you don't know the answer, just say that you don't know. 
Use three sentences maximum and keep the answer concise.
Question: {question} 
Context: {context} 
Answer:
"""
prompt = ChatPromptTemplate.from_template(template)

#print(prompt)
query = "What heat treatment is used for Steel SAE 1040?"

rag_chain = (
        {"context": retriever,
         "question": RunnablePassthrough()}
        | prompt
        | llm
        | StrOutputParser()
        )


result = rag_chain.invoke(query)
print(result)
When I try sample code given here:
from langchain.document_loaders import ConfluenceLoader

loader = ConfluenceLoader(
    url="<my confluence link>", username="<my user name>", 
    api_key="<my token>"
)
documents = loader.load(space_key="<my space>", include_attachments=True, limit=1, max_pages=1)

I get an error:
AttributeError: 'str' object has no attribute 'get'

Here is the last part of the stack:
    554     """
    555     Get all pages from space
    556 
   (...)
    568     :return:
    569     """
    570     return self.get_all_pages_from_space_raw(
    571         space=space, start=start, limit=limit, status=status, expand=expand, content_type=content_type
--> 572     ).get("results")

Any ideas? I see an issue here but it is still open.
I have now also opened bug specifically for this issue.
Here is the summary of the fixes required in the original code:

Do not suffix the URL with /wiki/home
suffix the user name with @ your domain name
use ID of the space as in the URL and not its display name

then it works. The error handling is poor to point to these issues otherwise.
I am confused by how multiple messages are combined and sent to a large language model such as ChatOpenAI.
from langchain_core.prompts import ChatPromptTemplate

template = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful AI bot. Your name is {name}."),
    ("human", "Hello, how are you doing?"),
    ("ai", "I'm doing well, thanks!"),
    ("human", "{user_input}"),
])

messages = template.format_messages(
    name="Bob",
    user_input="What is your name?"
)

messages

[SystemMessage(content='You are a helpful AI bot. Your name is Bob.'),
 HumanMessage(content='Hello, how are you doing?'),
 AIMessage(content="I'm doing well, thanks!"),
 HumanMessage(content='What is your name?')]

Is it generating text that looks like this:
System:
Human:
Assistant:
Human:
...

How can I print the final text sent to the llm?
I wanted to add additional metadata to the documents being embedded and loaded into Chroma.
I'm unable to find a way to add metadata to documents loaded using
Chroma.from_documents(documents, embeddings)
For example, imagine I have a text file having details of a particular disease, I wanted to add species as a metadata that is a list of all species it affects.
As a round-about way I loaded it in a chromadb collection by adding required metadata and persisted it
client = chromadb.PersistentClient(path="chromaDB")

collection = client.get_or_create_collection(name="test",
                                             embedding_function=openai_ef,
                                             metadata={"hnsw:space": "cosine"})

collection.add(
     documents=documents,
     ids=ids,
     metadatas=metadata
)

This was the result,
collection.get(include=['embeddings','metadatas'])

Output:

{'ids': ['id0',
'id1',
'embeddings': [[-0.014580891467630863,
0.0003901976451743394,
0.00793908629566431,
-0.027648288756608963,
-0.009689063765108585,
0.010222840122878551,
-0.00946609303355217,
-0.002771923551335931,
-0.04675614833831787,
-0.02056729979813099,
0.014364678412675858,
...
{'species': 'XYZ', 'source': 'Flu.txt'},
{'species': 'ABC', 'source': 'Common_cold.txt'}],
'documents': None,
'uris': None,
'data': None}

Now I tried loading it from the directory persisted in the disk using Chroma.from_documents()
db = Chroma(persist_directory="chromaDB", embedding_function=embeddings)

But I don't see anything loaded. db.get() results in this,
db.get(include=['metadatas'])

Output:

{'ids': [],
'embeddings': None,
'metadatas': [],
'documents': None,
'uris': None,
'data': None}

Please help. Need to load metadata to the files being loaded.
I want to create a local LLM using falcon 40b instruct model and combine it with lanchain so I can give it a pdf or some resource to learn from so I can query it ask it questions, learn from it and ultimately be able to derive insights from the pdf report from an Excel sheet.
For now, I just want to load a pdf using langchain and have the falcon-40b-instruct model as the agent.
I want to build an llm where I can make it interact with my own data using langchain.
Here is my attempt so far:
from langchain_community.llms import HuggingFaceHub

llm = HuggingFaceHub(
repo_id=model_name,
task="text-generation",
model_kwargs={
"max_new_tokens": 512,
"top_k": 30,
"temperature": 0.1,
"repetition_penalty": 1.03
},
huggingfacehub_api_token="hf_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)

I reached the following stage:
from langchain_community.chat_models.huggingface import ChatHuggingFace
llm = ChatHuggingFace(llm=llm)

yet I get this error:

HfHubHTTPError: 401 Client Error: Unauthorized for url

I am doing do this to be able to run the following:
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=vector_db.as_retriever()
)

What am I missing and is there a way to be able to do this fully local like doing the falcon model and pass it to ChatHuggingFace?
I am in the process of building a RAG like the one in this Video. However, I cannot import FAISS like this.
from langchain.vectorstores import FAISS

LangChainDeprecationWarning: Importing vector stores from langchain is deprecated. Importing from langchain will no longer be supported as of langchain==0.2.0. Please import from langchain-community instead:

`from langchain_community.vectorstores import faiss`.

However, it is possible to import faiss:
from langchain_community.vectorstores import faiss

But with this it is not possible to call faiss.from_text().
vectorstore = faiss.from_texts(text=text_chunks, embeddings=embeddings)

AttributeError: module 'langchain_community.vectorstores.faiss' has no attribute 'from_texts'

Is it no longer possible to call .from_text() with the current one? I didn't find anything about this in the documentation.
Python=3.10.13
I have a cluster, that is not connected to the internet, although has a sort of weights repository available. I need to run LLM inference on it.
The only option that I found until now is using combination of transformers and langchain modules, but I don't want to tweak hyperparameters of models. I ran into ollama software, but I cannot install anything on cluster, except from python libs. So, naturally I wonder, what are my options for running LLM inference? And there is some more questions.

Can I just install ollama-python package and not install their linux software? Or do I need both to run my inference?
If I manage to install ollama on this cluster, how can I provide pretrained weights to the model? If it helps, they are stored in (sometime multiple) .bin files