delayedkarma/langchain-issues huggingface dataset:
DOC: Data Pipeline for humans
Labels: name: auto:question

### Issue with current documentation:

Dear all

I have this pipeline

```python
translation_cache = ToJSON(key=key, out_dir=Path("results/sabadel/translation"))
translation_prompt = Prompt.from_yaml(Path("prompts/translate.yml"))
translation_chain = (
    {
        "transcription": lambda data: format_transcription_for_prompt(
            data["transcription"]
        )
    }
    | translation_prompt.template
    | model
    | {
        "transcription": RunnableLambda(
            lambda res: translation_output_parser(res, transcription)
        )
    }
    | {"transcription": lambda x: translation_cache(x)}
)

translation_chain = (
    RunnableLambda(lambda data: {**data, "transcription": translation_cache.load()})
    if translation_cache.exists
    else translation_chain
)

# evaluation
evaluation_prompt = Prompt.from_yaml(Path("prompts/sabadell/evaluation.yml"))
evaluation_cache = ToJSON(key=key, out_dir=Path("results/sabadel/evaluation"))
evaluation_chain = (
    evaluation_prompt.template | model | evaluation_output_parser | evaluation_cache
)

evaluation_chain = (
    RunnableLambda(lambda data: {**data, "evaluations": evaluation_cache.load()})
    if evaluation_cache.exists
    else evaluation_chain
)
# retention
retention_prompt = Prompt.from_yaml(Path("prompts/sabadell/evaluation.retention.yml"))
retention_cache = ToJSON(key=key, out_dir=Path("results/sabadel/retention"))
retention_chain = (
    retention_prompt.template | model | retention_output_parser | retention_cache
)
retention_chain = RunnableLambda(
    lambda data: {
        **data,
        "retention": retention_cache.load()
        if retention_cache.exists
        else retention_chain(**data),
    }
)
# final chain
# chain = translation_chain | retention_chain
# print(translation_chain.invoke({"transcription": transcription}))
chain = translation_chain | evaluation_chain | retention_chain
print(chain.get_graph().print_ascii())
print(
    chain.invoke({"transcription": transcription, "retention_script": retention_script})
)
```

Now what I'd like to do is that `evaluation_chain` puts stuff into a key `evaluations` and pass along the original data dict + that key to `retention_chain` and `retention_chain` should put it's output into a `retention` data key and then pass along the original dict + all the outputs

how to do it?


### Idea or request for content:

_No response_
Add doc for AstraDB document loader
Labels: name: lgtm, name: area: doc loader, name: auto:documentation, name: size:L

<!-- Thank you for contributing to LangChain!

Please title your PR "<package>: <description>", where <package> is whichever of langchain, community, core, experimental, etc. is being modified.

Replace this entire comment with:
  - **Description:** a description of the change, 
  - **Issue:** the issue # it fixes if applicable,
  - **Dependencies:** any dependencies required for this change,
  - **Twitter handle:** we announce bigger features on Twitter. If your PR gets announced, and you'd like a mention, we'll gladly shout you out!

Please make sure your PR is passing linting and testing before submitting. Run `make format`, `make lint` and `make test` from the root of the package you've modified to check this locally.

See contribution guidelines for more information on how to write/run tests, lint, etc: https://python.langchain.com/docs/contributing/

If you're adding a new integration, please include:
  1. a test for the integration, preferably unit tests that do not rely on network access,
  2. an example notebook showing its use. It lives in `docs/docs/integrations` directory.

If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17.
 -->
See preview : https://langchain-git-fork-cbornet-astra-loader-doc-langchain.vercel.app/docs/integrations/document_loaders/astradb
Fix AstraDB logical operator filtering
Labels: name: lgtm, name: area: vector store, name: auto:bug, name: size:M

<!-- Thank you for contributing to LangChain!

Please title your PR "<package>: <description>", where <package> is whichever of langchain, community, core, experimental, etc. is being modified.

Replace this entire comment with:
  - **Description:** a description of the change, 
  - **Issue:** the issue # it fixes if applicable,
  - **Dependencies:** any dependencies required for this change,
  - **Twitter handle:** we announce bigger features on Twitter. If your PR gets announced, and you'd like a mention, we'll gladly shout you out!

Please make sure your PR is passing linting and testing before submitting. Run `make format`, `make lint` and `make test` from the root of the package you've modified to check this locally.

See contribution guidelines for more information on how to write/run tests, lint, etc: https://python.langchain.com/docs/contributing/

If you're adding a new integration, please include:
  1. a test for the integration, preferably unit tests that do not rely on network access,
  2. an example notebook showing its use. It lives in `docs/docs/integrations` directory.

If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17.
 -->
This change fixes the AstraDB logical operator filtering (`$and,` `$or`).
The `metadata` prefix must not be added if the key is `$and` or `$or`.
langchain-docs: Correct the word "iteratively" in use-cases documentation
Labels: name: lgtm, name: auto:documentation, name: size:XS


Replace this entire comment with:
  - **Description:** a description of the change, 
  - **Issue:** the issue # it fixes if applicable,
  - **Dependencies:** any dependencies required for this change,
  - **Twitter handle:** we announce bigger features on Twitter. If your PR gets announced, and you'd like a mention, we'll gladly shout you out!
**Description:** Fixes the word "iteratively" in the use-cases documentation
**Twitter handle:** @untilhamza
Reuse KV-Cache with local LLM (IlamaCpp) instead of expensive reprocessing of all history tokens
Labels: name: auto:improvement, name: area: models

### Feature request

- I want the local LLM (IlamaCpp) to maintain its context, which will significantly improve the efficiency of follow-up questions.
- Currently, the context of IlamaCpp is lost after the first call, necessitating the reprocessing of all tokens for any subsequent question.
- **Proposed Solution:** Utilize the internal KV cache of IlamaCpp to retain context and avoid reprocessing the same tokens repeatedly.


### Motivation

- My motivation is to address the inefficiency in the current process where the context is not preserved between queries.
- There seems to be no existing solution for this specific issue as per my research, for example, [LangChain Caching Documentation](https://python.langchain.com/docs/modules/model_io/chat/chat_model_caching#in-memory-cache).

minimized example which shows my current workaround:
```

from langchain.llms import LlamaCpp
from langchain.chains import LLMChain
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
from pathlib import Path

class LlmRunner:
    def __init__(self, path_to_model: Path) -> None:
        self.llm_instance = LlamaCpp(
            model_path=str(path_to_model),
            n_ctx=16384,
            max_tokens=-1,
            temperature=0,
            repeat_penalty=1.15,
            n_gpu_layers=1,
            n_threads=8,
            verbose=False,
        )

    def run(self, invoice_text: str):
        # Initial processing filling up kv-cache and context
        initial_prompt = """
Extract and format keys from the invoice text into JSON.
<context>
{input}
</context>
        """
        chain1 = LLMChain(
            llm=self.llm_instance,
            prompt=ChatPromptTemplate.from_messages([
                HumanMessagePromptTemplate.from_template(initial_prompt)
            ])
        )
        response1 = chain1.invoke({'input': invoice_text})

        # Follow-up processing which COULD reuse the context, but doesn't
        follow_up_prompt = """
Review your results and normalize the dates to YYYY-MM-DD.
<input>
{input}
</input>

<context>
{invoice_text}
</context>"""
        chain2 = LLMChain(
            llm=self.llm_instance,
            prompt=ChatPromptTemplate.from_messages([
                HumanMessagePromptTemplate.from_template(follow_up_prompt)
            ])
        )
        response2 = chain2.invoke({'invoice_text' : invoice_text, 'input': response1['text']})

        return response2['text']

# Example usage
path_to_model = Path("path_to_your_model")
runner = LlmRunner(path_to_model)
invoice_text = "Your invoice text here"
result = runner.run(invoice_text)
print(result)

```

### Your contribution

- if something like this already exists I am willing to provide example and update documentation
- if you point me in the right directions and it's just a few 100s LOC I am willing to submit a PR
feat: add a flag that determines whether to load the milvus collection
Labels: name: area: vector store, name: auto:improvement

### Feature request

Every time i create a milvus object, i load the collection, but there is no way to dynamically know the replica_number of the currently loaded collection, so there is a disadvantage that i have to hand over the different replica_number for each collection as an argument. Therefore, when creating a milvus object, I would like to add a flag that can determine whether to load or not.

### Motivation

Always loading a collection can cause an unexpected error.

### Your contribution

https://github.com/langchain-ai/langchain/pull/15693
feat: add a flag that determines whether to load the milvus collection
Labels: name: lgtm, name: area: vector store, name: auto:improvement, name: size:S

<!-- Thank you for contributing to LangChain!

Please title your PR "<package>: <description>", where <package> is whichever of langchain, community, core, experimental, etc. is being modified.

Replace this entire comment with:
  - **Description:** a description of the change, 
  - **Issue:** the issue # it fixes if applicable,
  - **Dependencies:** any dependencies required for this change,
  - **Twitter handle:** we announce bigger features on Twitter. If your PR gets announced, and you'd like a mention, we'll gladly shout you out!

Please make sure your PR is passing linting and testing before submitting. Run `make format`, `make lint` and `make test` from the root of the package you've modified to check this locally.

See contribution guidelines for more information on how to write/run tests, lint, etc: https://python.langchain.com/docs/contributing/

If you're adding a new integration, please include:
  1. a test for the integration, preferably unit tests that do not rely on network access,
  2. an example notebook showing its use. It lives in `docs/docs/integrations` directory.

If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17.
 -->
fix https://github.com/langchain-ai/langchain/issues/15694
community: Improve mlflow callback 
Labels: name: auto:improvement, name: size:L, name: partner

- **Description:** Allow passing run_id to MLflowCallbackHandler to resume a run instead of creating a new run. Support recording retriever relevant metrics. Refactor the code to fix some bugs.

Please make sure your PR is passing linting and testing before submitting. Run `make format`, `make lint` and `make test` from the root of the package you've modified to check this locally.

See contribution guidelines for more information on how to write/run tests, lint, etc: https://python.langchain.com/docs/contributing/
Add Dynamic Schema support for the Milvus vector store
Labels: name: area: vector store, name: auto:improvement

### Feature request

I suggest supporting the Milvus vector database's new [Dynamic Schema](https://milvus.io/docs/dynamic_schema.md) feature.


### Motivation

According to Milvus:

> Dynamic schema enables users to insert entities with new fields into a Milvus collection without modifying the existing schema. This means that users can insert data without knowing the full schema of a collection and can include fields that are not yet defined.

I think it is good to allow Langchain to have this feature when multiple types or schema of documents are added to the database.

### Your contribution

I propose to add a "dynamic_schema" flag to the `__init__` and `from_texts` method of the Milvus class:

`__init__` method:

https://github.com/langchain-ai/langchain/blob/4c47f39fcb539fdeff6dd6d9b1f483cd9a1af69b/libs/community/langchain_community/vectorstores/milvus.py#L107-L125

Change to:

```python
    def __init__(
        self,
        embedding_function: Embeddings,
        collection_name: str = "LangChainCollection",
        collection_description: str = "",
        connection_args: Optional[dict[str, Any]] = None,
        consistency_level: str = "Session",
        index_params: Optional[dict] = None,
        search_params: Optional[dict] = None,
        drop_old: Optional[bool] = False,
        *,
        primary_field: str = "pk",
        text_field: str = "text",
        vector_field: str = "vector",
        metadata_field: Optional[str] = None,
        partition_names: Optional[list] = None,
        replica_number: int = 1,
        timeout: Optional[float] = None,
        dynamic_schema = False,
    ):
```

`from_texts` method:

https://github.com/langchain-ai/langchain/blob/4c47f39fcb539fdeff6dd6d9b1f483cd9a1af69b/libs/community/langchain_community/vectorstores/milvus.py#L839-L887

Change to:

```python
    def from_texts(
        cls,
        texts: List[str],
        embedding: Embeddings,
        metadatas: Optional[List[dict]] = None,
        collection_name: str = "LangChainCollection",
        connection_args: dict[str, Any] = DEFAULT_MILVUS_CONNECTION,
        consistency_level: str = "Session",
        index_params: Optional[dict] = None,
        search_params: Optional[dict] = None,
        drop_old: bool = False,
        dynamic_schema = False,
        **kwargs: Any,
    ) -> Milvus:
```

I may later submit a PR for this suggestion.
Issue: <How can I extract vector data from pgvector for use as a reference in the next conversation to enable long-term memory functionality for my chatbot?>
Labels: name: area: vector store, name: auto:bug

### Issue you'd like to raise.

import os
from urllib.parse import quote_plus
from langchain.vectorstores.pgvector import PGVector
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationTokenBufferMemory
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores.pgvector import DistanceStrategy

os.environ['OPENAI_API_KEY'] = "mykey"
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")

CONNECTION_STRING = PGVector.connection_string_from_db_params(
driver=os.environ.get("PGVECTOR_DRIVER", "psycopg2"),
host="192.168.xxx.xx",
port=5432,
database="xxxxx",
user="xxxxxxxxx",
password=quote_plus("xxxx@r"),
)

vectordb = PGVector(embedding_function=embeddings,
collection_name="tmp06",
connection_string=CONNECTION_STRING,
distance_strategy=DistanceStrategy.COSINE,
)

llm_name = "gpt-3.5-turbo"
llm = ChatOpenAI(model_name=llm_name, temperature=0)
memory_token_limit = 100
retriever = vectordb.as_retriever()
memory = ConversationTokenBufferMemory(
llm=llm,
max_token_limit=int(memory_token_limit),
memory_key="chat_history",
return_messages=True
)
qa = ConversationalRetrievalChain.from_llm(
llm,
retriever=retriever,
memory=memory,
verbose=True,
)
chat_history = []
while True:
memory.load_memory_variables({})
question = input('ask：')
result = qa.run({'question': question, 'chat_history': chat_history})
print(result)
chat_history.append([f'User: {question}', f'Ai: {result}'])
print(chat_history)
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
st_history = ' '.join(map(str, chat_history))
res = embeddings.embed_query(st_history)
vectordb.add_vector(res)
best_solution = vectordb.nearest(res, n=1)

print(f'ok: {res[:4]}...')
if question.lower() == 'bye':
    break
Traceback (most recent call last):
File "C:\Users\syz\Downloads\ChatBotgpt-3.5-turbo-main\models\1227.py", line 53, in
vectordb.add_vector(res)
^^^^^^^^^^^^^^^^^^^
AttributeError: 'PGVector' object has no attribute 'add_vector'

### Suggestion:

How can I extract vector data from pgvector for use as a reference in the next conversation to enable long-term memory functionality for my chatbot?
No close() functionality in  langchain.sql_database import SQLDatabase package
Labels: name: auto:improvement

### Feature request

from langchain_experimental.sql import SQLDatabaseChain
from langchain.sql_database import SQLDatabase
I'm using the above packages to connect the databricks database(SQLDatabse)and passing it to the model chain(SQLDatabaseChain) to generate the SQLQuery. But I want to close the connection of the database after each response. I couldn't find anything to close the database connection using this SQLDatabase package. Even in the SQLDatabase documentation I couldn't find anything. So I need some close() function to close the connection of the database.

### Motivation

Because of this close() functionality not available in the SQLDatabase package. I'm getting (sqlalchemy.exc.OperationalError) so I need to reboot the server to tackle this issue but that was not the feasible solution. And one more thing I can't use other different packages to connect my database because the model chain accept only the SQLDatabase in the parameter. 

### Your contribution

Try to add the close() functionality in the SQLDatabase.py file so the database connection can be closed. So that I'll not be facing any issues in the future. 
Thanks in advance.
Translate User Query and Model Response in RetrievalQA Chain
Labels: name: auto:improvement, name: area: models

Hi,

I have built a RAG app with RetrievalQA and now wanted to try out a new approach. I am using an English LLM but the responses should be in German. E.g. if the user asks something in German "Hallo, wer bist du?", the user query should be translated to "Hello, who are you?" before feeding it into the RAG pipeline. 
After the model made its response in English "I am an helpful assistant" the output should be translated back to German "Ich bin ein hilfreicher Assistent".

As translator I am using `googletrans==3.1.0a0`

Here is my RetrievalQA Chain:

```
from langchain.chains import RetrievalQA
from langchain.memory import ConversationBufferWindowMemory
import box
import yaml
from src.utils import set_prompt, setup_dbqa, build_retrieval_qa
from src.llm import build_llm
from src.prompts import mistral_prompt
from langchain.vectorstores import FAISS

with open('config/config.yml', 'r', encoding='utf8') as ymlfile:
    cfg = box.Box(yaml.safe_load(ymlfile))

def build_retrieval_qaa(llm, prompt, vectordb):
    chain_type_kwargs={ "prompt": prompt,
                        "memory": ConversationBufferWindowMemory(
                            memory_key="chat_history",
                            input_key="question",
                            #output_key="answer",
                            k=8,
                            return_messages=True),
                        "verbose": False
                        }
    dbqa = RetrievalQA.from_chain_type(llm=llm,
                                        chain_type='stuff',
                                        retriever=vectordb,
                                        return_source_documents=cfg.RETURN_SOURCE_DOCUMENTS,
                                        chain_type_kwargs=chain_type_kwargs,
                                        verbose=False
                                        )
    return dbqa

llm = build_llm(ANY LLM)
qa_prompt = set_prompt(mistral_prompt)
vectordb = FAISS.load_local(cfg.DB_FAISS_PATH, bge_embeddings)
vectordb = vectordb.as_retriever(search_kwargs={'k': cfg.VECTOR_COUNT, 'score_treshold': cfg.SCORE_TRESHOLD}, search_type="similarity")
dbqa = build_retrieval_qaa(llm, qa_prompt, vectordb)
dbqa("Was bedeutet IPv6 für die Software-Entwicklung?") # Gives me a response
```

The prompt looks like this: 
```
mistral_prompt = """

 
 Du bist RagBot, ein hilfsbereiter Assistent. Antworte nur auf Deutsch. Verwende die folgenden Kontextinformationen, um die Frage am Ende zu beantworten. Wenn du die Antwort nicht kennst, sag einfach, dass du es nicht weisst. Versuche nicht eine Antwort zu erfinden.

###Chat History###: {chat_history}

###Kontext###: {context} 

###Frage###: {question} 
Antwort: 

"""
```

So what do I have to change here, to first translate the user query and the prompt from DE to EN, and afterwards the Model response from EN to DE? Specifically I have problems to translate the provided context, chat history and question.
Issue: <How can I store 'res' in a vector database, and have a vector retrieval query for the best solution every time there's an input, to achieve long-term memory for OpenAI responses? Please help me modify this string: ' prefix>
Labels: name: area: vector store, name: auto:question

### Issue you'd like to raise.

import os
from urllib.parse import quote_plus
from langchain.vectorstores.pgvector import PGVector
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationTokenBufferMemory
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores.pgvector import DistanceStrategy

os.environ['OPENAI_API_KEY'] = "mykey"
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")

CONNECTION_STRING = PGVector.connection_string_from_db_params(
    driver=os.environ.get("PGVECTOR_DRIVER", "psycopg2"),
    host="192.168.xxx.xx",
    port=5432,
    database="xxxxx",
    user="xxxxxxxxx",
    password=quote_plus("xxxxxr"),
)

vectordb = PGVector(embedding_function=embeddings,
                    collection_name="tmp06",
                    connection_string=CONNECTION_STRING,
                    distance_strategy=DistanceStrategy.COSINE,
                    )

llm_name = "gpt-3.5-turbo"
llm = ChatOpenAI(model_name=llm_name, temperature=0)
memory_token_limit = 100
retriever = vectordb.as_retriever()
memory = ConversationTokenBufferMemory(
    llm=llm,
    max_token_limit=int(memory_token_limit),
    memory_key="chat_history",
    return_messages=True
)
qa = ConversationalRetrievalChain.from_llm(
    llm,
    retriever=retriever,
    memory=memory,
    verbose=True,
)
chat_history = []
while True:
    memory.load_memory_variables({})
    question = input('ask：')
    result = qa.run({'question': question, 'chat_history': chat_history})
    print(result)
    chat_history.append([f'User: {question}', f'Ai: {result}'])
    print(chat_history)
    embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
    st_history = ' '.join(map(str, chat_history))
    res = embeddings.embed_query(st_history)         ########################ˇ

    print(f'ok: {res[:4]}...')
    if question.lower() == 'bye':
        break
How can I store 'res' in a vector database, and have a vector retrieval query for the best solution every time there's an input, to achieve long-term memory for OpenAI responses? Please help me modify this string     translates to "Please help me see if there are any errors" in English

### Suggestion:

How can I store 'res' in a vector database, and have a vector retrieval query for the best solution every time there's an input, to achieve long-term memory for OpenAI responses? Please help me modify this string     translates to "Please help me see if there are any errors" in English