delayedkarma/langchain-issues huggingface dataset:
jinja2 formate suppot trim_blocks mode
Labels: name: auto:improvement

### Feature request

when i use jinja2 template， i get the following the problem
template:
```
{% if a %}\n
{{a}}\n
{% endif %}\n
{{b}}
```
and i pass b variable to template render,then the output like this
```
\n
b
```
Each variable placeholder is followed by a  '\n'. 
Since a is not passed in, and '\n' is left after rendering

i wand the right output is this:
```
b
```
in jinja2 ,use enviroment create template ,and enable trim_blocks ,the jinja2 can ignore '\n'.

this is origin code 
```python
def jinja2_formatter(template: str, **kwargs: Any) -> str:
    """Format a template using jinja2."""
    try:
        from jinja2 import Template
    except ImportError:
        raise ImportError(
            "jinja2 not installed, which is needed to use the jinja2_formatter. "
            "Please install it with `pip install jinja2`."
        )

    return Template(template).render(**kwargs)
```
I tried modifying it a bit
```python
def jinja2_formatter(template: str, **kwargs: Any) -> str:
    """Format a template using jinja2."""
    try:
        from jinja2 import Environment
    except ImportError:
        raise ImportError(
            "jinja2 not installed, which is needed to use the jinja2_formatter. "
            "Please install it with `pip install jinja2`."
        )

    env = Environment(trim_blocks=True) 
    jinja_template = env.from_string(template) 

    return jinja_template.render(**kwargs) 
```





### Motivation

I often use the control semantics of Jinja2 to construct prompt templates, creating several different prompts by passing in different variables. 

However, since the Jinja2 render in Langchain does not support trim_blocks, it always results in unnecessary blank lines. 

Perhaps LLM doesn't care about these extra lines, but as a human being, my OCD makes me feel uncomfortable with them.

### Your contribution

The jinja2_formatter function is too low-level within the Langchain code. 

Although the modification is simple, I fear that the PR I submit could potentially affect other parts of the code.

So, if you have time, perhaps you could personally implement this feature.
PipelinePromptTemplate  format_prompt should support missing keyword
Labels: name: auto:improvement

### Feature request

the following is part of  the format code for PipelinePromptTemplate
```python
def _get_inputs(inputs: dict, input_variables: List[str]) -> dict:
    return {k: inputs[k] for k in input_variables}
class PipelinePromptTemplate(BasePromptTemplate):
  ....
  def format_prompt(self, **kwargs: Any) -> PromptValue:
      for k, prompt in self.pipeline_prompts:
          _inputs = _get_inputs(kwargs, prompt.input_variables)
          if isinstance(prompt, BaseChatPromptTemplate):
              kwargs[k] = prompt.format_messages(**_inputs)
          else:
              kwargs[k] = prompt.format(**_inputs)
      _inputs = _get_inputs(kwargs, self.final_prompt.input_variables)
      return self.final_prompt.format_prompt(**_inputs)
```
the function _get_inputs should support miss keyword process，like:
```python
def _get_inputs(inputs: dict, input_variables: List[str]) -> dict:
    return {k: inputs.get(k) for k in input_variables}
```

### Motivation

I primarily use the Jinja2 template format and frequently use control statements. Consequently, there are times when I choose not to pass in certain variables. Thus, I hope that PipelinePromptTemplate could support handling missing variables.

### Your contribution

Perhaps, I can submit a PR to add this feature or fix this bug . If you don't have the time to address it, that is.
The Fellowship of the Vectors: New Embeddings Filter using clustering.
Labels: name: auto:enhancement, name: auto:documentation

Continuing with Tolkien inspired series of langchain tools. I bring to you:
**The Fellowship of the Vectors**, AKA EmbeddingsClusteringFilter.
This document filter uses embeddings to group vectors together into clusters, then allows you to pick an arbitrary number of documents vector based on proximity to the cluster centers. That's a representative sample of the cluster.

The original idea is from [Greg Kamradt](https://github.com/gkamradt) from this video (Level4):
https://www.youtube.com/watch?v=qaPMdcCqtWk&t=365s

I added few tricks to make it a bit more versatile, so you can parametrize what to do with duplicate documents in case of cluster overlap: replace the duplicates with the next closest document or remove it. This allow you to use it as an special kind of redundant filter too.
Additionally you can choose 2 diff orders:  grouped by cluster or respecting the original retriever scores.
In my use case I was using the docs grouped by cluster to run refine chains per cluster to generate summarization over a large corpus of documents.
Let me know if you want to change anything!

@rlancemartin, @eyurtsev, @hwchase17,
Support params on GoogleSearchApiWrapper (#6810)
Labels: name: lgtm, name: auto:improvement

## Description
Support search params in GoogleSearchApiWrapper's result call, for the extra filtering on search,
to support extra query parameters that google cse provides:
https://developers.google.com/custom-search/v1/reference/rest/v1/cse/list?hl=ko

## Issue
#6810 

## Dependencies
None

## Maintainer
@dev2049
openapi chain nit
Labels: name: auto:nit

<!-- Thank you for contributing to LangChain!

Replace this 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!

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.

Maintainer responsibilities:
  - General / Misc / if you don't know who to tag: @dev2049
  - DataLoaders / VectorStores / Retrievers: @rlancemartin, @eyurtsev
  - Models / Prompts: @hwchase17, @dev2049
  - Memory: @hwchase17
  - Agents / Tools / Toolkits: @vowelparrot
  - Tracing / Callbacks: @agola11
  - Async: @agola11

If no one reviews your PR within a few days, feel free to @-mention the same people again.

See contribution guidelines for more information on how to write/run tests, lint, etc: https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
 -->
How to save embeddings when using Agent for local document Q&A to avoid unnecessary waste by rereading API calls to generate embeddings
Labels: name: auto:improvement

Attach my code

```
llm = OpenAI(model_name='gpt-4', temperature=0)
embeddings = OpenAIEmbeddings(model='text-embedding-ada-002')

docs = TextLoader("state_of_the_union.txt").load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(docs)
state_of_union_store = Chroma.from_documents(
   texts, 
   embeddings, 
   collection_name="state-of-union"
   )

vectorstore_info = VectorStoreInfo(
   name="state_of_union_address",
   description="the most recent state of the Union adress",
   vectorstore=state_of_union_store,
)
toolkit = VectorStoreToolkit(vectorstore_info=vectorstore_info)
agent_executor = create_vectorstore_agent(llm=llm, toolkit=toolkit, verbose=True)

agent_executor("What did biden say about ketanji brown jackson in the state of the union address?")
```
How to add memory with` create_vectorstore_agent`
Labels: name: auto:question

I want to add memory context for Agent, so I referred to the document [here](https://python.langchain.com/docs/modules/memory/how_to/agent_with_memory)

There is such a step in it
![image](https://github.com/hwchase17/langchain/assets/49063302/17005b7e-05f1-47b5-9d41-9766601bf56e)

I want to implement the same step in the `create_vectorstore_agent `function, 
![image](https://github.com/hwchase17/langchain/assets/49063302/a7491872-2d23-4a14-b0ca-6be987523ee1)

but I found that `create_vectorstore_agent `is a pre-defined, 
it has no way to pass some parameters, such as suffix(into `ZeroShotAgent.create_prompt`), 
which means, if I want to use the `agent_with_memory `function , 
I must build the agent from scratch, and cannot use `create_vectorstore_agent`
python tool issue: NameError: name 're' is not defined
Labels: name: auto:bug

### System Info

I am using a create_pandas_dataframe_agent like so:

```
create_pandas_dataframe_agent(ChatOpenAI(model_name='gpt-4', temperature=0), df, verbose=True,
                                         max_iterations=2,
                                         early_stopping_method="generate",
                                         handle_parsing_errors="I'm sorry, but the question you have asked is not available within the dataset. Is there anything else I can help you with?")
```

When I try to prompt it to do anything that requires a regex the output i get:

```
Thought: The client wants to know the top 5 hashtags used in the tweets. To find this, I need to extract all the hashtags from the 'full_text' column of the dataframe, count the frequency of each hashtag, and then return the top 5. However, the dataframe does not contain a column for hashtags. Therefore, I need to create a new column 'hashtags' in the dataframe where each entry is a list of hashtags found in the corresponding 'full_text'. Then, I can count the frequency of each hashtag in this new column and return the top 5.

Action: python_repl_ast
Action Input:
```python
import re
import pandas as pd

# Function to extract hashtags from a text
def extract_hashtags(text):
    return re.findall(r'\#\w+', text)

# Create a new column 'hashtags' in the dataframe
df['hashtags'] = df['full_text'].apply(extract_hashtags)

# Flatten the list of hashtags and count the frequency of each hashtag
hashtag_counts = pd.Series([hashtag for hashtags in df['hashtags'] for hashtag in hashtags]).value_counts()

# Get the top 5 hashtags
top_5_hashtags = hashtag_counts.head(5)

top_5_hashtags
```

**Observation: NameError: name 're' is not defined**
```

Can anyone help?

### 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
- [X] Tools / Toolkits
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

use this agent:
create_pandas_dataframe_agent(ChatOpenAI(model_name='gpt-4', temperature=0), df, verbose=True,
                                         max_iterations=2,
                                         early_stopping_method="generate",
                                         handle_parsing_errors="I'm sorry, but the question you have asked is not available within the dataset. Is there anything else I can help you with?")

then prompt it with

top 5 hashtags used

### Expected behavior

the regex should work and return the answer
Update get_started.mdx
Labels: name: lgtm, name: auto:bug, name: auto:documentation

typo in chat = ChatOpenAI(open_api_key="...") should be openai_api_key

<!-- Thank you for contributing to LangChain!

Replace this 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!

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.

Maintainer responsibilities:
  - General / Misc / if you don't know who to tag: @dev2049
  - DataLoaders / VectorStores / Retrievers: @rlancemartin, @eyurtsev
  - Models / Prompts: @hwchase17, @dev2049
  - Memory: @hwchase17
  - Agents / Tools / Toolkits: @vowelparrot
  - Tracing / Callbacks: @agola11
  - Async: @agola11

If no one reviews your PR within a few days, feel free to @-mention the same people again.

See contribution guidelines for more information on how to write/run tests, lint, etc: https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
 -->
ElasticSearch embedding need a function to add single text string as AzureOpenAI only support single text input now
Labels: name: auto:improvement

### Feature request

ElasticSearch embedding need a function to add single text string as Azure OpenAI only support single text input now.

Message from Azure OpenAI Embedding:

embed_documents(texts)

    # openai.error.InvalidRequestError: Too many inputs for model None.
    # The max number of inputs is 1.  We hope to increase the number of inputs per request soon.
    # Please contact us through an Azure support request at: https://go.microsoft.com/fwlink/?linkid=2213926 for further questions.

### Motivation

To support to use Azure OpenAI embedding API

### Your contribution

I can do more test after if this fixed.
FAISS Support for filter while using the as_retriever()
Labels: name: auto:enhancement

### Feature request

[https://python.langchain.com/docs/modules/data_connection/vectorstores/integrations/faiss](https://python.langchain.com/docs/modules/data_connection/vectorstores/integrations/faiss)

There's a section that allows you to filter the documents stored in FAISS
It will be cool to allow
results = db.similarity_search("foo", filter=dict(page=1), k=1, fetch_k=4)
when you are doing 
vectorstore.as_retriever(<<Include the filter here>>)
This can allow you to select a subset of your vector documents to chat with them.

This is if I have multiple documents from multiple sources loaded in the vectorstroe.


### Motivation

When I do a conversational


            qa = ConversationalRetrievalChain(
                    retriever=vector..as_retriever(), ...

If allow to use only a portion of the document 


            qa = ConversationalRetrievalChain(
                    retriever=vector.as_retriever(<<Filter goes here>>>), ...



### Your contribution

I can test it :-)
Unable to load OPENAI Embeddings
Labels: name: auto:bug

### System Info

Most recent versions of all

### Who can help?

@hwchase17 @agola11 

### Information

- [ ] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [ ] LLMs/Chat Models
- [X] 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

    from langchain.embeddings.openai import OpenAIEmbeddings
  File "C:\Users\hasaa\anaconda3\lib\site-packages\langchain\__init__.py", line 6, in <module>
    from langchain.agents import MRKLChain, ReActChain, SelfAskWithSearchChain
  File "C:\Users\hasaa\anaconda3\lib\site-packages\langchain\agents\__init__.py", line 2, in <module>
    from langchain.agents.agent import (
  File "C:\Users\hasaa\anaconda3\lib\site-packages\langchain\agents\agent.py", line 30, in <module>
    from langchain.prompts.few_shot import FewShotPromptTemplate
  File "C:\Users\hasaa\anaconda3\lib\site-packages\langchain\prompts\__init__.py", line 12, in <module>
    from langchain.prompts.example_selector import (
  File "C:\Users\hasaa\anaconda3\lib\site-packages\langchain\prompts\example_selector\__init__.py", line 4, in <module>
    from langchain.prompts.example_selector.semantic_similarity import (
  File "C:\Users\hasaa\anaconda3\lib\site-packages\langchain\prompts\example_selector\semantic_similarity.py", line 10, in <module>
    from langchain.vectorstores.base import VectorStore
  File "C:\Users\hasaa\anaconda3\lib\site-packages\langchain\vectorstores\__init__.py", line 2, in <module>
    from langchain.vectorstores.alibabacloud_opensearch import (
  File "C:\Users\hasaa\anaconda3\lib\site-packages\langchain\vectorstores\alibabacloud_opensearch.py", line 9, in <module>
    from langchain.vectorstores.base import VectorStore
  File "C:\Users\hasaa\anaconda3\lib\site-packages\langchain\vectorstores\base.py", line 372, in <module>
    class VectorStoreRetriever(BaseRetriever, BaseModel):
  File "C:\Users\hasaa\anaconda3\lib\site-packages\langchain\vectorstores\base.py", line 388, in VectorStoreRetriever
    def validate_search_type(cls, values: Dict) -> Dict:
  File "pydantic\class_validators.py", line 126, in pydantic.class_validators.root_validator.dec
  File "pydantic\class_validators.py", line 144, in pydantic.class_validators._prepare_validator
pydantic.errors.ConfigError: duplicate validator function "langchain.vectorstores.base.VectorStoreRetriever.validate_search_type"; if this is intended, set `allow_reuse=True`

### Expected behavior

The error I am facing is due to a duplicate validator function in the VectorStoreRetriever
DOC: <Please write a comprehensive title after the 'DOC: ' prefix>
Labels: name: auto:bug, name: auto:documentation

### Issue with current documentation:

You can get chat completions by passing one or more messages to the chat model. The response will be a message. The types of messages currently supported in LangChain are AIMessage, HumanMessage, SystemMessage, and ChatMessage -- ChatMessage takes in an arbitrary role parameter. Most of the time, you'll just be dealing with HumanMessage, AIMessage, and SystemMessage.
```
from langchain.chat_models import ChatOpenAI
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)

chat = ChatOpenAI(temperature=0)
chat.predict_messages([HumanMessage(content="Translate this sentence from English to French. I love programming.")])
# >> AIMessage(content="J'aime programmer.", additional_kwargs={})
```

It is useful to understand how chat models are different from a normal LLM, but it can often be handy to just be able to treat them the same. LangChain makes that easy by also exposing an interface through which you can interact with a chat model as you would a normal LLM. You can access this through the predict interface.
```
chat.predict("Translate this sentence from English to French. I love programming.")
# >> J'aime programmer
```

### Idea or request for content:

This code gives me the error:
```
AttributeError: 'ChatOpenAI' object has no attribute 'predict'
```
Is there really an exposed interface for predict?
HuggingFaceTextGenInference does not accept 0 temperature
Labels: name: auto:bug

### System Info


running google/flan-tf-xxl on ghcr.io/huggingface/text-generation-inference:0.8

langchain==0.0.220
text-generation==0.6.0

### Who can help?

@hwchase17 
@agola11 

### Information

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

### Related Components

- [X] 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

`HuggingFaceTextGenInference` does not accept a temperature parameter of 0 or None.

```
llm = HuggingFaceTextGenInference(
    inference_server_url="http://localhost:8010/",
    temperature=0.
)
llm("What did foo say about bar?")
```

ValidationError: `temperature` must be strictly positive


```
llm = HuggingFaceTextGenInference(
    inference_server_url="http://localhost:8010/",
    temperature=None
)
llm("What did foo say about bar?")
```

ValidationError: 1 validation error for HuggingFaceTextGenInference
temperature none is not an allowed value (type=type_error.none.not_allowed)

### Expected behavior

I expect to be able to pass a parameter to `HuggingFaceTextGenInference` that instructs the model to do greedy decoding without getting an error.

It seems like the issue is that LangChain enforces that temperature be an integer while the text-generation [client requires temperature](https://github.com/huggingface/text-generation-inference/blob/main/clients/python/text_generation/types.py#L74C4-L74C4) be None for greedy decoding or >0 for sampling.
Replace JIRA Arbitrary Code Execution vulnerability with finer grain API wrapper
Labels: name: auto:improvement

This fixes #4833 and the critical vulnerability https://nvd.nist.gov/vuln/detail/CVE-2023-34540

Previously, the JIRA API Wrapper had a mode that simply pipelined user input into an `exec()` function.
[The intended use of the 'other' mode is to cover any of Atlassian's API that don't have an existing interface](https://github.com/hwchase17/langchain/blob/cc33bde74ff2e050a400e4451e04ff5b32c4a7bd/langchain/tools/jira/prompt.py#L24)

Fortunately all of the [Atlassian JIRA API methods are subfunctions of their `Jira` class](https://atlassian-python-api.readthedocs.io/jira.html), so this implementation calls these subfunctions directly.

As well as passing a string representation of the function to call, the implementation flexibly allows for optionally passing args and/or keyword-args. These are given as part of the dictionary input. Example:
```
    {
        "function": "update_issue_field",   #function to execute
        "args": [                           #list of ordered args similar to other examples in this JiraAPIWrapper
            "key",
            {"summary": "New summary"}
        ],
        "kwargs": {}                        #dict of key value keyword-args pairs
    }
```

the above is equivalent to `self.jira.update_issue_field("key", {"summary": "New summary"})`

Alternate query schema designs are welcome to make querying easier without passing and evaluating arbitrary python code. I considered parsing (without evaluating) input python code and extracting the function, args, and kwargs from there and then pipelining them into the callable function via `*f(args, **kwargs)` - but this seemed more direct.

@vowelparrot @dev2049
langchain takes much time to reload
Labels: name: auto:bug

### System Info

Macos system

### 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
- [X] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

loading malfunction
Pack of small fixes and refactorings that don't affect functionality
Labels: name: auto:nit, name: auto:refactor

Description: Pack of small fixes and refactorings that don't affect functionality, just making code prettier & fixing some misspelling (hand-filtered improvements proposed by SeniorAi.online, prototype of code improving tool based on gpt4), agents and callbacks folders was covered.

Dependencies: Nothing changed

Twitter: https://twitter.com/nayjest

@dev2049
@vowelparrot
@agola11
added `Brave Search` document_loader
Labels: name: auto:enhancement, name: auto:improvement, name: auto:documentation

- Added `Brave Search` document loader.
- Refactored BraveSearch wrapper
- Added a Jupyter Notebook example
- Added `Ecosystem/Integrations` BraveSearch page 

Please review:
  - DataLoaders / VectorStores / Retrievers: @rlancemartin, @eyurtsev