delayedkarma/langchain-issues huggingface dataset:
Add MongoDBAtlasVectorSearch vectorstore

### Feature request

MongoDB Atlas is a fully managed DBaaS, powered by the MongoDB database. It also enables Lucene (collocated with the mongod process) for full-text search - this is know as Atlas Search. The PR has to allow Langchain users from using the functionality related to the MongoDB Atlas Vector Search feature where you can store your embeddings in MongoDB documents and create a Lucene vector index to perform a KNN search.

### Motivation

There is currently no way in Langchain to connect to MongoDB Atlas and perform a KNN search.

### Your contribution

I am submitting a PR for this issue soon.
FAISS.add_embeddings is typed to take iterables but does not. 

### System Info

MacOS
Langchain Version 0.0.181
Python Version 3.11.3

### Who can help?

@eyurtsev I wasn't sure who to reach out to. The following is the signature for adding embeddings to FAISS: 

```python
FAISS.add_embeddings(
    self,
    text_embeddings: 'Iterable[Tuple[str, List[float]]]',
    metadatas: 'Optional[List[dict]]' = None,
    **kwargs: 'Any',
) -> 'List[str]'
```

Notice that `text_embeddings` takes an iterable. However, when I do this I get a failure with my iterable, but when wrapped in a `list` function then it is successful.

 

### Information

- [ ] 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

```python
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings

vs = FAISS.from_texts(['a'], embedding=OpenAIEmbeddings())
vector = OpenAIEmbeddings().embed_query('b')

# error happens with this next line, see "Expected behavior" below.
vs.add_embeddings(iter([('b', vector)]))

# no error happens when wrapped in a list
vs.add_embeddings(list(iter([('b', vector)])))
```

### Expected behavior

```bash
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
...
File ~/.pyenv/versions/3.11.3/envs/myenv/lib/python3.11/site-packages/faiss/class_wrappers.py:227, in handle_Index.<locals>.replacement_add(self, x)
    214 def replacement_add(self, x):
    215     """Adds vectors to the index.
    216     The index must be trained before vectors can be added to it.
    217     The vectors are implicitly numbered in sequence. When `n` vectors are
   (...)
    224         `dtype` must be float32.
    225     """
--> 227     n, d = x.shape
    228     assert d == self.d
    229     x = np.ascontiguousarray(x, dtype='float32')

ValueError: not enough values to unpack (expected 2, got 1)
```
Improved json parse sanitization

@vowelparrot  Added regex expressions (superset to the previously present non regex expressions) to also solve the case where unnecessary characters are present after the triple backticks but before the {, and after the } but before the closing triple backticks.

Before fix (additional "AI: " is present after Thought:):
```
Observation: There are 38175 accounts available in the dataframe.
Thought:AI: {
    "action": "Final Answer",
    "action_input": "There are 38175 accounts available in the dataframe."
}
Observation: Invalid or incomplete response
```

After fix:
```
Observation: There are 38175 accounts available in the dataframe.
Thought:AI: {
    "action": "Final Answer",
    "action_input": "There are 38175 accounts available in the dataframe."
}

> Finished chain.
[AI Message]: There are 38175 accounts available in the dataframe.
```
refactor: BaseStringMessagePromptTemplate from_template method 
Labels: name: lgtm


# refactor BaseStringMessagePromptTemplate from_template method 

Refactor the `from_template` method of the `BaseStringMessagePromptTemplate` class to allow passing keyword arguments to the `from_template` method of `PromptTemplate`.
Enable the usage of arguments like `template_format`.
In my scenario, I intend to utilize Jinja2 for formatting the human message prompt in the chat template.

## Who can review?

Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested:

<!-- For a quicker response, figure out the right person to tag with @

  @hwchase17 - project lead

  Models
  - @hwchase17
  - @agola11
  - @jonasalexander 

 -->
Fixing blank thoughts in verbose for "_Exception" Action

Fixed the issue of blank Thoughts being printed in verbose when `handle_parsing_errors=True`, as below:

Before Fix:
```
Observation: There are 38175 accounts available in the dataframe.
Thought:
Observation: Invalid or incomplete response
Thought:
Observation: Invalid or incomplete response
Thought:
```

After Fix:
```
Observation: There are 38175 accounts available in the dataframe.
Thought:AI: {
    "action": "Final Answer",
    "action_input": "There are 38175 accounts available in the dataframe."
}
Observation: Invalid Action or Action Input format
Thought:AI: {
    "action": "Final Answer",
    "action_input": "The number of available accounts is 38175."
}
Observation: Invalid Action or Action Input format
```

@vowelparrot currently I have set the colour of thought to green (same as the colour when `handle_parsing_errors=False`). If you want to change the colour of this "_Exception" case to red or something else (when `handle_parsing_errors=True`), feel free to change it in line 789.
Serp APi and google search API won't work with LLama models like vicuna

### System Info

Langchain Version: 0.0.176
Ubuntu x86 23.04
Memory 24gb
AMD EPYC

### Who can help?

_No response_

### Information

- [ ] 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
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

```
# Callbacks support token-wise streaming
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
# Verbose is required to pass to the callback manager

# Make sure the model path is correct for your system!
llm_cpp = LlamaCpp(model_path="/vicuna/ggml-vic7b-q4_0.bin", callback_manager=callback_manager)
llm = llm_cpp
tools = load_tools(["serpapi"], llm=llm_cpp)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
agent.run("What is football?")
```
Result
```
Action Input: "what is football?" I should probably start by defining what football actually is.
Action: Let's [Search] "what is football?"
Action Input: "what is football?"
Observation: Let's [Search] "what is football?" is not a valid tool, try another one.
Thought:
```

### Expected behavior

Expected behavior:
search google and return correct results

If i change model from vicuna to openAI api, works fine
Difference among various ways to query database and return source information? (Question Answering with Sources, Retrieval Question Answering with Sources, index.query_with_sources, etc.)

### Issue you'd like to raise.

What are the key differences between these methods of trying to query/ask a database, then return the answer along with its relevant sources?

My main objective is to have a Chatbot that has knowledge from a knowledge base, and can still maintain conversation history. Their answer must return me the source document as well. Which option is the best among so many choices?

There are

1. [Question Answering with Sources](https://python.langchain.com/en/latest/modules/chains/index_examples/qa_with_sources.html), 
```
chain = load_qa_with_sources_chain(OpenAI(temperature=0), chain_type="stuff")
query = "What did the president say about Justice Breyer"
chain({"input_documents": docs, "question": query}, return_only_outputs=True)
```

2. [Retrieval Question Answering with Sources](https://python.langchain.com/en/latest/modules/chains/index_examples/vector_db_qa_with_sources.html)
```
from langchain import OpenAI

chain = RetrievalQAWithSourcesChain.from_chain_type(OpenAI(temperature=0), chain_type="stuff", retriever=docsearch.as_retriever())

chain({"question": "What did the president say about Justice Breyer"}, return_only_outputs=True)
```

3. [Question Answering over Docs](https://python.langchain.com/en/latest/use_cases/question_answering.html)
```
from langchain.indexes import VectorstoreIndexCreator
index = VectorstoreIndexCreator().from_loaders([loader])

query = "What did the president say about Ketanji Brown Jackson"
index.query_with_sources(query)
```

++ probably quite a few more examples I could find if I dig through the documentation.
Add path validation to DirectoryLoader

# Add path validation to DirectoryLoader

This PR introduces a minor adjustment to the DirectoryLoader by adding validation for the path argument. Previously, if the provided path didn't exist or wasn't a directory, DirectoryLoader would return an empty document list due to the behavior of the `glob` method. This could potentially cause confusion for users, as they might expect a file-loading error instead.

So, I've added two validations to the load method of the DirectoryLoader:

- Raise a FileNotFoundError if the provided path does not exist
- Raise a ValueError if the provided path is not a directory

Due to the relatively small scope of these changes, a new issue was not created.

## Before submitting

<!-- If you're adding a new integration, please include:

1. a test for the integration - favor unit tests that does not rely on network access.
2. an example notebook showing its use


See contribution guidelines for more information on how to write tests, lint
etc:

https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->

## Who can review?

Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested:

@eyurtsev
Issue: Fix or automate sync of releases to Discord Announcements channel, Twitter, etc.

### Issue you'd like to raise.

LC Release announcements seem to be missing from Discord's Announcements channel since 0.0.166. Looking more closely, these seem to be manual, added by hwchase17.

On Twitter, the most recent release announcement from the LangChainAI account is 0.0.170, viz:

https://twitter.com/search?q=(from%3ALangChainAI)%20release&src=typed_query&f=live


### Suggestion:

I couldn't tell from this project's various actions whether such postings are meant to be automated upon release (Github search on actions isn't great), and just need to be fixed. If not, I think it would be would be very useful for the community to add such release notification actions, so that the various places people keep up to date are all, well, up to date.
Allow ElasticsearchEmbeddings to create a connection with ES Client object
Labels: name: lgtm

This PR adds a new method `from_es_connection` to the `ElasticsearchEmbeddings` class allowing users to use Elasticsearch clusters outside of Elastic Cloud. 

Users can create an Elasticsearch Client object and pass that to the new function. 
The returned object is identical to the one returned by calling `from_credentials`

```
# Create Elasticsearch connection
es_connection = Elasticsearch(
    hosts=['https://es_cluster_url:port'], 
    basic_auth=('user', 'password')
)

# Instantiate ElasticsearchEmbeddings using es_connection
embeddings = ElasticsearchEmbeddings.from_es_connection(
  model_id,
  es_connection,
)
```

I also added examples to the elasticsearch jupyter notebook

Fixes # https://github.com/hwchase17/langchain/issues/5239


cc: @hwchase17
fix: remove empty lines that cause InvalidRequestError


# remove empty lines in GenerativeAgentMemory that cause InvalidRequestError in OpenAIEmbeddings

<!--
Thank you for contributing to LangChain! Your PR will appear in our release under the title you set. Please make sure it highlights your valuable contribution.

Replace this with a description of the change, the issue it fixes (if applicable), and relevant context. List any dependencies required for this change.

After you're done, someone will review your PR. They may suggest improvements. If no one reviews your PR within a few days, feel free to @-mention the same people again, as notifications can get lost.
-->

<!-- Remove if not applicable -->

Let's say the text given to `GenerativeAgent._parse_list` is
```
text = """
Insight 1: <insight 1>

Insight 2: <insight 2>
"""
```
This creates an `openai.error.InvalidRequestError: [''] is not valid under any of the given schemas - 'input'` because `GenerativeAgent.add_memory()` tries to add an empty string to the vectorstore.

This PR fixes the issue by removing the empty line between `Insight 1` and `Insight 2`

## Before submitting

<!-- If you're adding a new integration, please include:

1. a test for the integration - favor unit tests that does not rely on network access.
2. an example notebook showing its use


See contribution guidelines for more information on how to write tests, lint
etc:

https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->

## Who can review?

Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested:

<!-- For a quicker response, figure out the right person to tag with @

  @hwchase17 - project lead

  Tracing / Callbacks
  - @agola11

  Async
  - @agola11

  DataLoaders
  - @eyurtsev

  Models
  - @hwchase17
  - @agola11

  Agents / Tools / Toolkits
  - @vowelparrot

  VectorStores / Retrievers / Memory
  - @dev2049
        
 -->
@hwchase17
@vowelparrot
@dev2049
Fixes and doc updates for DeepInfra integration.
Labels: name: lgtm

# Your PR Title (What it does)

<!--
Thank you for contributing to LangChain! Your PR will appear in our release under the title you set. Please make sure it highlights your valuable contribution.

Replace this with a description of the change, the issue it fixes (if applicable), and relevant context. List any dependencies required for this change.

After you're done, someone will review your PR. They may suggest improvements. If no one reviews your PR within a few days, feel free to @-mention the same people again, as notifications can get lost.
-->

<!-- Remove if not applicable -->

Fixes # (issue)

## Before submitting

<!-- If you're adding a new integration, please include:

1. a test for the integration - favor unit tests that does not rely on network access.
2. an example notebook showing its use


See contribution guidelines for more information on how to write tests, lint
etc:

https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->

## Who can review?

Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested:

  - @hwchase17
  - @agola11
VertexAIEmbeddings error when passing a list with of length greater than 5.

### System Info

google-cloud-aiplatform==1.25.0
langchain==0.0.181
python 3.10

### Who can help?

_No response_

### Information

- [ ] The official example notebooks/scripts
- [X] My own modified scripts

### Related Components

- [ ] LLMs/Chat Models
- [X] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

Any list with len > 5 will cause an error.

```python
from langchain.vectorstores import FAISS
from langchain.embeddings import VertexAIEmbeddings

text = ['text_1', 'text_2', 'text_3', 'text_4', 'text_5', 'text_6']
embeddings = VertexAIEmbeddings()
vectorstore = FAISS.from_texts(text, embeddings)
```
```python

InvalidArgument                           Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/google/api_core/grpc_helpers.py](https://localhost:8080/#) in error_remapped_callable(*args, **kwargs)
     72             return callable_(*args, **kwargs)
     73         except grpc.RpcError as exc:
---> 74             raise exceptions.from_grpc_error(exc) from exc
     75 
     76     return error_remapped_callable

InvalidArgument: 400 5 instance(s) is allowed per prediction. Actual: 6
```


### Expected behavior

Excepted to successfully be able to vectorize a larger list of items. Maybe implement a step to
minor refactor GenerativeAgentMemory

# minor refactor of GenerativeAgentMemory

<!--
Thank you for contributing to LangChain! Your PR will appear in our release under the title you set. Please make sure it highlights your valuable contribution.

Replace this with a description of the change, the issue it fixes (if applicable), and relevant context. List any dependencies required for this change.

After you're done, someone will review your PR. They may suggest improvements. If no one reviews your PR within a few days, feel free to @-mention the same people again, as notifications can get lost.
-->

<!-- Remove if not applicable -->

- refactor `format_memories_detail` to be more reusable
- modified prompts for getting topics for reflection and for generating insights
- update `characters.ipynb` to reflect changes

## Before submitting

<!-- If you're adding a new integration, please include:

1. a test for the integration - favor unit tests that does not rely on network access.
2. an example notebook showing its use


See contribution guidelines for more information on how to write tests, lint
etc:

https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->

## Who can review?

Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested:

<!-- For a quicker response, figure out the right person to tag with @

  @hwchase17 - project lead

  Tracing / Callbacks
  - @agola11

  Async
  - @agola11

  DataLoaders
  - @eyurtsev

  Models
  - @hwchase17
  - @agola11

  Agents / Tools / Toolkits
  - @vowelparrot

  VectorStores / Retrievers / Memory
  - @dev2049
        
 -->
@vowelparrot
@hwchase17
@dev2049
Converting ChatOpenAI model to ONNX format

### Feature request

Is there any way to convert ChatOpenAI models to ONNX format? I've noticed that other models can be converted to ONNX (example: https://github.com/openai/whisper/discussions/134) and I was wondering if similar logic could be applied in this case as well.

### Motivation

I want to save these models in the ONNX format (a single file) so I can easily retrieve them and use them for question-answering. I want to be able to save the model as a single file in this case.

### Your contribution

Not sure. I could create a PR if I'm able to succeed in this.
Update Prediction Guard LLM wrapper

# Update Prediction Guard LLM wrapper to the latest version/ functionality

No dependencies updates here, but updating the LLM wrapper for [Prediction Guard](https://www.predictionguard.com/) to the latest version of the Python client, which includes additional functionality. Specifically the new version includes functionality to:

- control/ structure the output of LLMs
- access the latest open access LLMs (e.g., MPT 7B Instruct) with an OpenAI like API

## Who can review?

Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @hwchase17 or @vowelparrot (as they reviewed the original integration PR for Prediction Guard).

Thanks in advance!
Add Spark DataFrame as a Document Loader

# Add Spark DataFrame as a Document Loader

This is currently a work in progress PR on adding Spark DataFrames as a Document Loader **(tests haven't been added yet**). Langchain already has a Pandas DF loader and so extended support for Spark seemed to be the next step. The core issue is that Spark DataFrames are usually not just stored on one worker, so instead of doing a major code change to allow for `yield` functionality with Document Loaders, I simply just checked how much memory is available and set the max size for this DocumentLoader list as a certain fraction of it. It is currently set to 1/2 but it should be set to something like 1/10 or 1/20 for regular usage.
docs: improve flow of llm caching notebook

# docs: improve flow of llm caching notebook

The notebook `llm_caching` demos various caching providers. In the previous version, there was setup common to all examples but under the `In Memory Caching` heading.

If a user comes and only wants to try a particular example, they will run the common setup, then the cells for the specific provider they are interested in. Then they will get import and variable reference errors. This commit moves the common setup to the top to avoid this.

## Who can review?

Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested:

@dev2049
Add a langchain.embeddings.AnthropicEmbeddings class

### Feature request

Add a langchain.embeddings.AnthropicEmbeddings class, similar to the langchain.embeddings.OpenAIEmbeddings class



### Motivation

I am trying to modify this notebook to use Claude by Anthropic instead of OpenAI: https://github.com/pinecone-io/examples/blob/master/generation/langchain/handbook/05-langchain-retrieval-augmentation.ipynb 

This notebook uses Pinecone and an OpenAI LLM to do retrieval augmentation, but I would like to use Claude by Anthropic

However, I am stuck because of the lack of a corresponding langchain.embeddings.AnthropicEmbeddings to replace the langchain.embeddings.OpenAIEmbeddings class that is used in this example

### Your contribution

I am willing to contribute, but would appreciate some guidance. I am very new to this project
Fix: Handle empty documents in ContextualCompressionRetriever (Issue #5304)

# Fix: Handle empty documents in ContextualCompressionRetriever (Issue #5304)

Fixes #5304 

Prevent cohere.error.CohereAPIError caused by an empty list of documents by adding a condition to check if the input documents list is empty in the compress_documents method. If the list is empty, return an empty list immediately, avoiding the error and unnecessary processing.

@dev2049
Add SKLearnVectorStore
Labels: name: enhancement, name: area: vector store

# Add SKLearnVectorStore

This PR adds SKLearnVectorStore, a simply vector store based on NearestNeighbors implementations in the scikit-learn package. This provides a simple drop-in vector store implementation with minimal dependencies (scikit-learn is typically installed in a data scientist / ml engineer environment). The vector store can be persisted and loaded from json, bson and parquet format. 

SKLearnVectorStore has soft (dynamic) dependency on the scikit-learn, numpy and pandas packages. Persisting to bson requires the bson package, persisting to parquet requires the pyarrow package.

## Before submitting

Integration tests are provided under `tests/integration_tests/vectorstores/test_sklearn.py`

Sample usage notebook is provided under `docs/modules/indexes/vectorstores/examples/sklear.ipynb`

## Who can review?

Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested:

@hwchase17 - project lead
        
VectorStores / Retrievers / Memory
 @dev2049