delayedkarma/langchain-issues huggingface dataset:
LRU cache for import to avoid repetetive calls to huggingface
Labels: name: auto:improvement, name: area: models

# Description
Performance improvement to avoid repeated calls to huggingface when calling `RetrievalQAWithSourcesChain.from_chain_type`. By caching the call to the underlying transformers library, we avoid re-importing the library, instantiating the object and pulling data from huggingface. This significantly speeds up the chain (in our situation from 9 seconds to 3 seconds)

We noticed that our chain was spending a significant time pulling files from huggingface _on every LLM call_
![Screenshot 2023-08-28 at 18 53 50](https://github.com/langchain-ai/langchain/assets/2284951/1f850740-b9aa-4ac2-8880-e85e04870e0e)
![Screenshot 2023-08-28 at 18 54 00](https://github.com/langchain-ai/langchain/assets/2284951/a7024616-01ce-4d54-bfaf-162f2736a0aa)


Twitter: @PascalBrokmeier


<!-- Thank you for contributing to LangChain!

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,
  - Tag maintainer: for a quicker response, tag the relevant maintainer (see below),
  - 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` to check this locally.

See contribution guidelines for more information on how to write/run tests, lint, etc: 
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md

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. These live is docs/extras directory.

If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17, @rlancemartin.
 -->
llama.cpp does not support AsyncCallbackHandler
Labels: name: auto:bug, name: area: models

### System Info

python:3.11.3
langchain: 0.0.274
dist: arch

### Who can help?

```
fastapi==0.103.0
qdrant-client==1.4.0
llama-cpp-python==0.1.72
```

### Information

- [X] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [ ] LLMs/Chat Models
- [ ] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [ ] Chains
- [X] Callbacks/Tracing
- [X] Async

### Reproduction

Hey @agola11,
I got this runtime warning: 
`python3.11/site-packages/langchain/llms/llamacpp.py:312: RuntimeWarning: coroutine 'AsyncCallbackManagerForLLMRun.on_llm_new_token' was never awaited`

I try to stream over a websocket the generated tokens. When I try to add a `AsyncCallbackHandler` to manage this streaming and run `acall` the warning occurs and nothing is streamed out.




```python
class StreamingLLMCallbackHandler(AsyncCallbackHandler):
    def __init__(self, websocket: WebSocket):
        super().__init__()
        self.websocket = websocket

    async def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
        resp = ChatResponse(sender="bot", message=token, type="stream")
        await self.websocket.send_json(resp.dict())


stream_manager = AsyncCallbackManager([StreamingLLMCallbackHandler(websocket)])



llm = return LlamaCpp(
                model_path="models/llama-2-7b-chat.ggmlv3.q2_K.bin",
                verbose=False,
                n_gpu_layers=40,
                n_batch=2048,
                n_ctx=2048,
                callback_manager=stream_manager
            )

qa_chain = RetrievalQA.from_chain_type(llm=llm,
                                       chain_type='stuff',
                                       retriever=vectordb.as_retriever(search_kwargs={'k': 4}),
                                       return_source_documents=True,
                                       chain_type_kwargs={'prompt': prompt}
                                       )

output = await qa_chain.acall(
                {
                    'query': user_msg,
                })
```




### Expected behavior

The expected behavior is that each token is streamed sequentially over the websocket.
Add root run wrapping call to RunnableEach()
Labels: name: auto:improvement

<!-- Thank you for contributing to LangChain!

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,
  - Tag maintainer: for a quicker response, tag the relevant maintainer (see below),
  - 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` to check this locally.

See contribution guidelines for more information on how to write/run tests, lint, etc: 
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md

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. These live is docs/extras directory.

If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17, @rlancemartin.
 -->
Add data anonymizer
Labels: name: auto:enhancement

### Description

The feature for anonymizing data has been implemented. In order to protect private data, such as when querying external APIs (OpenAI), it is worth pseudonymizing sensitive data to maintain full privacy.

Anonynization consists of two steps:

1. **Identification:** Identify all data fields that contain personally identifiable information (PII).
2. **Replacement**: Replace all PIIs with pseudo values or codes that do not reveal any personal information about the individual but can be used for reference. We're not using regular encryption, because the language model won't be able to understand the meaning or context of the encrypted data.

We use *Microsoft Presidio* together with *Faker* framework for anonymization purposes because of the wide range of functionalities they provide. The full implementation is available in `PresidioAnonymizer`.

### Future works

- **deanonymization** - add the ability to reverse anonymization. For example, the workflow could look like this: `anonymize -> LLMChain -> deanonymize`. By doing this, we will retain anonymity in requests to, for example, OpenAI, and then be able restore the original data.
- **instance anonymization** - at this point, each occurrence of PII is treated as a separate entity and separately anonymized. Therefore, two occurrences of the name John Doe in the text will be changed to two different names. It is therefore worth introducing support for full instance detection, so that repeated occurrences are treated as a single object.

### Twitter handle
@deepsense_ai / @MaksOpp
AWS Sagemaker JS Integration
Labels: name: area: embeddings, name: auto:question, name: area: models

### System Info

JS LangChain

### Who can help?

_No response_

### Information

- [X] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [X] LLMs/Chat Models
- [X] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [X] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction


@dosu-bot

I got this error now with my code: ModelError: Received client error (424) from primary with message "{
"code":424,
"message":"prediction failure",
"error":"Need to pass custom_attributes='accept_eula=true' as part of header. This means you have read and accept the end-user license agreement (EULA) of the model. EULA can be found in model card description or from https://ai.meta.com/res

```
const { SageMakerLLMContentHandler, SageMakerEndpoint } = require("langchain/llms/sagemaker_endpoint");
const AWS = require('aws-sdk');

AWS.config.credentials = new AWS.SharedIniFileCredentials({ profile: 'profile' });

class HuggingFaceTextGenerationGPT2ContentHandler {
  constructor(contentHandler) {
    this.contentHandler = contentHandler;
    this.contentType = "application/json";
    this.accepts = "application/json";
  }

  async transformInput(prompt, modelKwargs) {
    const inputString = JSON.stringify({
      text_inputs: prompt,
      ...modelKwargs,
    });
    return Buffer.from(inputString);
  }

  async transformOutput(output) {
    const responseJson = JSON.parse(Buffer.from(output).toString("utf-8"));
    return responseJson.generated_texts[0];
  }
}

const contentHandler = new HuggingFaceTextGenerationGPT2ContentHandler(SageMakerLLMContentHandler);

const model = new SageMakerEndpoint({
  endpointName: "endpointName",
  modelKwargs: { temperature: 1e-10 },
  contentHandler: contentHandler, // Pass the inner contentHandler
  clientOptions: {
    region: "region",
    credentials: AWS.config.credentials,
  },
});

async function main() {
  const res = await model.call("Hello, my name is ");
  console.log({ res });
}

main();


```

Can you show where to add the eula agreement acceptanced parameter



### Expected behavior

I expected it to pass, but it asked me to accept a EULA Agreement. I tried customized arguments but that didn't seem to solve hte issue either
Support index upsert parallelization for pinecone
Labels: name: area: embeddings, name: area: vector store, name: auto:improvement

### Feature request

We can take advantage from pinecone parallel upsert (see example: https://docs.pinecone.io/docs/insert-data#sending-upserts-in-parallel)

This will require modification of the current `from_texts` pipeline to
1. Create a batch (chunk) for doing embeddings (ie have a chunk size of 1000 for embeddings)
2. Perform a parallel upsert to Pinecone index on that chunk

This way we are in control on 3 things:
1. Thread pool for pinecone index
2. Parametrize the batch size for embeddings (ie it helps to avoid rate limit for OpenAI embeddings)
3. Parametrize the batch size for upsert (it helps to avoid throttling of pinecone API)

As a part of this ticket, we can consolidate the code between `add_texts` and `from_texts` as they are doing the similar thing.

### Motivation

The function `from_text` and `add_text` for index upsert doesn't take advantage of parallelism especially when embeddings are calculated by HTTP calls (ie OpenAI embeddings). This makes the whole sequence inefficient from IO bound standpoint as the pipeline is following:
1. Take a small batch ie 32/64 of documents
2. Calculate embeddings --> WAIT
3. Upsert a batch --> WAIT

We can benefit from either parallel upsert or we can utilize `asyncio`.

### Your contribution

I will do it.
AttributeError: module 'langchain' has no attribute 'verbose'
Labels: name: area: memory, name: area: embeddings, name: area: doc loader, name: area: vector store, name: auto:bug, name: area: models

### System Info

274
Mac M2

this error appears often and unexpectedly 
but gets solved temporarily by running a force reinstall

`pip install --upgrade --force-reinstall --no-deps --no-cache-dir langchain
`

full error
```
2023-08-28 08:16:48.197 Uncaught app exception
Traceback (most recent call last):
  File "/Users/user/Developer/newfilesystem/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
  File "/Users/user/Developer/newfilesystem/pages/chat.py", line 104, in <module>
    llm = ChatOpenAI(
  File "/Users/user/Developer/newfilesystem/venv/lib/python3.10/site-packages/langchain/load/serializable.py", line 74, in __init__
    super().__init__(**kwargs)
  File "pydantic/main.py", line 339, in pydantic.main.BaseModel.__init__
  File "pydantic/main.py", line 1066, in pydantic.main.validate_model
  File "pydantic/fields.py", line 439, in pydantic.fields.ModelField.get_default
  File "/Users/user/Developer/newfilesystem/venv/lib/python3.10/site-packages/langchain/chat_models/base.py", line 49, in _get_verbosity
    return langchain.verbose
```

main librairies in the project
requests streamlit pandas colorlog python-dotenv tqdm fastapi uvicorn
langchain openai tiktoken chromadb pypdf
colorlog logger docx2txt  

### Who can help?

@hwchase17

### Information

- [ ] The official example notebooks/scripts
- [X] My own modified scripts

### Related Components

- [X] LLMs/Chat Models
- [X] Embedding Models
- [X] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [X] Document Loaders
- [X] Vector Stores / Retrievers
- [X] Memory
- [X] Agents / Agent Executors
- [X] Tools / Toolkits
- [X] Chains
- [X] Callbacks/Tracing
- [ ] Async

### Reproduction

intermittent - no pattern

### Expected behavior

resolution
Constraining the search using 'where_filter' in case of similarity_score_threshold for langchain Weaviate wrapper
Labels: name: area: vector store, name: auto:bug

### System Info
https://github.com/langchain-ai/langchain/blob/610f46d83aae6e1e25d76a0222b3158e2c4f7034/libs/langchain/langchain/vectorstores/weaviate.py

Issue in Langchain Weaviate Wrapper.
Trying to constraint the search in case of `similarity_score_threshold` ignoring the `where_filter ` filters in the langchain Weaviate wrapper. 

`where_filter` implementation is missing for `similarity_score_threshold`. 

The issue is with `similarity_search_with_score` function. 

The fix would be to add the following after line 346, the point after the query_obj is initialized.

`        if kwargs.get("where_filter"): 
            query_obj = query_obj.with_where(kwargs.get("where_filter"))`

This would integrate the `where_filter` in constraints of the query if defined by the user.

### Who can help?

@hwchase17 
@rohitgr7 
@baskaryan 
@leo-gan 

### Information

- [X] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [ ] LLMs/Chat Models
- [ ] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [X] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction
https://github.com/langchain-ai/langchain/blob/610f46d83aae6e1e25d76a0222b3158e2c4f7034/libs/langchain/langchain/vectorstores/weaviate.py

Issue in Langchain Weaviate Wrapper.
Trying to constraint the search in case of `similarity_score_threshold` ignoring the `where_filter ` filters in the langchain Weaviate wrapper. 

`where_filter` implementation is missing for `similarity_score_threshold`. 


### Expected behavior
https://github.com/langchain-ai/langchain/blob/610f46d83aae6e1e25d76a0222b3158e2c4f7034/libs/langchain/langchain/vectorstores/weaviate.py

The fix would be to add the following after line 346, the point after the query_obj is initialized.

`        if kwargs.get("where_filter"): 
            query_obj = query_obj.with_where(kwargs.get("where_filter"))`

This would integrate the `where_filter` in constraints of the query if defined by the user.
Update only notebook
Labels: name: auto:documentation

<!-- Thank you for contributing to LangChain!

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,
  - Tag maintainer: for a quicker response, tag the relevant maintainer (see below),
  - 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` to check this locally.

See contribution guidelines for more information on how to write/run tests, lint, etc: 
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md

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. These live is docs/extras directory.

If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17, @rlancemartin.
 -->
Virtual column name for self-querying retrievers
Labels: name: area: vector store, name: auto:improvement, name: size:XL

<!-- Thank you for contributing to LangChain!

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,
  - Tag maintainer: for a quicker response, tag the relevant maintainer (see below),
  - 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` to check this locally.

See contribution guidelines for more information on how to write/run tests, lint, etc: 
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md

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. These live is docs/extras directory.

If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17, @rlancemartin.
 -->

Self query retrievers are powerful and it can be even stronger than before. Taking MyScale as an example, sometimes the user may add complex SQL function to a column name. So we add a `VirtualColumnName` for self-query retriever to reduce token usage with extra functionality.

1. a function that dynamically creates function mapped column names, which becomes handy when the user want to compare between current time / location or other attributes along with a column.
2. complex function call in plain string can now be replaced with a shorter nickname in prompt, which saves number of token for very long function names.
3. refined the logic in MyScale translator to meet this standard.

This change will not affect other self query retrievers as it preserves the original plain string interface under comparison. The actual difference happens under `QueryTransformer` in `query_construct.parser` for `Lark` parser.

`Comparator.attribute` can now be either a string or a `VirtualColumnName`. I have defined the default behaviour which stops the user to use `VirtualColumnName` under other vectorstores than MyScale.

This functionality will boost the self-query retrievers as a bridge between text and simple SQL query. And we believe this will help users to expand their usage to this retriever. Other SQL vector database is theoretically compatible to this new feature as well.

Hope you find this interesting.
@hwchase17  @baskaryan 

Our contact on twitter: @MyScaleDB @mpsk_liu
Extend SQLChatMessageHistory
Labels: name: area: memory, name: lgtm, name: auto:improvement

### Description

There is a really nice class for saving chat messages into a database - SQLChatMessageHistory.
It leverages SqlAlchemy to be compatible with any supported database (in contrast with PostgresChatMessageHistory, which is basically the same but is limited to Postgres).

However, the class is not really customizable in terms of what you can store. I can imagine a lot of use cases, when one will need to save a message date, along with some additional metadata. 

To solve this, I propose to extract the converting logic from BaseMessage to SQLAlchemy model (and vice versa) into a separate class - message converter. So instead of rewriting the whole SQLChatMessageHistory class, a user will only need to write a custom model and a simple mapping class, and pass its instance as a parameter.

I also noticed that there is no documentation on this class, so I added that too, with an example of custom message converter.

### Issue

N/A

### Dependencies

N/A

### Tag maintainer

Not yet

### Twitter handle

N/A
How to connect MS-SQL with LANG-CHAIN
Labels: name: auto:bug

### Issue you'd like to raise.

db = SQLDatabase.from_uri(
"mysql+pyodbc://Driver={SQL Server};Server=DESKTOP-17L7UI1\SQLEXPRESS;Database=DociQDb;rusted_Connection=yes;",)

I am trying to connect to my microsoft sql server but this give me error 
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('IM010', '[IM010] [Microsoft][ODBC Driver Manager] Data source name too long (0) (SQLDriverConnect)')


### Suggestion:

_No response_
Vearch vectorstore
Labels: name: lgtm, name: area: vector store, name: auto:enhancement

<!-- Thank you for contributing to LangChain!

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,
  - Tag maintainer: for a quicker response, tag the relevant maintainer (see below),
  - 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` to check this locally.

See contribution guidelines for more information on how to write/run tests, lint, etc: 
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md

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. These live is docs/extras directory.

If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17, @rlancemartin.
 -->
Mazharsaif patch 1
Labels: name: auto:documentation

<!-- Thank you for contributing to LangChain!

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,
  - Tag maintainer: for a quicker response, tag the relevant maintainer (see below),
  - 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` to check this locally.

See contribution guidelines for more information on how to write/run tests, lint, etc: 
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md

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. These live is docs/extras directory.

If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17, @rlancemartin.
 -->
docs: Fix spelling mistakes in Etherscan.ipynb