delayedkarma/langchain-issues huggingface dataset:
SQLDatabase object returns empty list with get_usable_table_names() 
Labels: name: area: agent, name: auto:bug, name: stale, name: area: models

### System Info

Langchain 0.0.264
sqlalchemy 1.4.39
Platform mac os ventura 13.2.1
Python 3.11.4


### Who can help?

_No response_

### Information

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

### Reproduction

1. from langchain.utilities import SQLDatabase 
2. create a connection string (conn) with mssql dialect
3. create a connection using  db = SQLDatabase.from_uri(conn) 
4. use db.get_usable_table_names() to print table names 
5. you will see an empty list returning 

### Expected behavior

it should return table names from the database hosted in mssql
Use PyPI Trusted Publishing to publish langchain packages.
Labels: name: auto:improvement

Trusted Publishing is the current best practice for publishing Python packages. Rather than long-lived secret keys, it uses OpenID Connect (OIDC) to allow our GitHub runner to directly authenticate itself to PyPI and get a short-lived publishing token. This locks down publishing quite a bit:
- There's no long-lived publish key to steal anymore.
- Publishing is *only* allowed via the *specifically designated* GitHub workflow in the designated repo.

It also is operationally easier: no keys means there's nothing that needs to be periodically rotated, nothing to worry about leaking, and nobody can accidentally publish a release from their laptop because they happened to have PyPI keys set up.

After this gets merged, we'll need to configure PyPI to start expecting trusted publishing. It's only a few clicks and should only take a minute; instructions are here: https://docs.pypi.org/trusted-publishers/adding-a-publisher/

More info:
- https://blog.pypi.org/posts/2023-04-20-introducing-trusted-publishers/
- https://github.com/pypa/gh-action-pypi-publish
Added Geometry Validation, Geometry Metadata, and WKT instead of Python str() to GeoDataFrame Loader
Labels: name: lgtm, name: area: doc loader, name: auto:improvement

@rlancemartin The current implementation within `Geopandas.GeoDataFrame` loader uses the python builtin `str()` function on the input geometries.  While this looks very close to WKT (Well known text), Python's str function doesn't guarantee that.

In the interest of  interop., I've changed to the of use `wkt` property on the Shapely geometries for generating the text representation of the geometries.

Also, included here:
- validation of the input `page_content_column` as being a GeoSeries.
- geometry `crs` (Coordinate Reference System) / bounds (xmin/ymin/xmax/ymax) added to Document metadata.  Having the CRS is critical... having the bounds is just helpful!

I think there is a larger question of "Should the geometry live in the `page_content`, or should the record be better summarized and tuck the geom into metadata?"  ...something for another day and another PR.
Add Functionality to Update Embeddings in pgvector
Labels: name: area: vector store, name: auto:enhancement

### Feature request

I propose an enhancement to the pgvector functionality: the addition of an update feature in [pgvector.py](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/vectorstores/pgvector.py). As it stands, the code only seems to allow for the addition of new embeddings. I believe a specific function, potentially named `PGVector.update_documents()`, would greatly improve the utility of this module.

### Motivation

The inspiration for this proposal comes from an opportunity I've identified in my project to enhance efficacy and efficiency. I believe that the ability to update embeddings when one of my documents changes would streamline the process significantly. The current workaround of deleting the entire collection or removing all embeddings for the target document to save new ones, though functional, leaves room for improvement. This feature would not only optimize my project but could also benefit other users of pgvector who might face a similar need.

### Your contribution

I am prepared and eager to contribute to the creation of this feature. The `PGVector.update_documents()` function I propose would:
1. Retrieve the current list of embeddings for the document and load it into memory.
2. Obtain the chunks of the updated document.
3. Compare the updated document's chunks with the existing embeddings in memory to identify matches, potentially saving unnecessary model calls for existing embeddings. If the embedding doesn't exist in memory, the model would be called to generate the embedding.
4. Issue an update to the DB for any changing embeddings, an insert for any new ones, and a delete for any missing embeddings.
I am willing to submit a PR for this feature upon receiving feedback from the langchain maintainers.
langchain.schema.output_parser.OutputParserException: Could not parse function call data: Invalid control character Using JsonOutputFunctionParser
Labels: name: auto:bug, name: area: models

The following code throws ```langchain.schema.output_parser.OutputParserException``` 90% of the time:

```python
    model = ChatOpenAI(model='gpt-4-0613') # code generation on gpt-3.5 isn't strong enough
    prompt = ChatPromptTemplate(messages=[
        SystemMessagePromptTemplate.from_template("Write code to solve the users problem. the last line of the python  program should print the answer. Do not use sympy"),
        HumanMessagePromptTemplate.from_template(f"What is the {n}th prime"),
    ])

    class PythonExecutionEnvironment(BaseModel):
        valid_python: str
        code_explanation: str

    python_repl = {"name": "python_repl", "parameters": PythonExecutionEnvironment.model_json_schema()}

    chain = prompt | model.bind(
        function_call={"name": python_repl["name"]}, functions=[python_repl]
    )  | JsonOutputFunctionsParser2()
    response = chain.invoke({})

```
The reason is gpt-4 is returning control charectors in it's python code, which is making the parser throw: 

https://github.com/langchain-ai/langchain/blob/50b8f4dcc722eb2ec5eccb17c25f1d3895442caa/libs/langchain/langchain/output_parsers/openai_functions.py#L44

The following resolves, but I'm not sure if there are other consequences:
```python
return json.loads(function_call_info, strict=False)
 ```
 More details from Stack Overflow
 
*    https://stackoverflow.com/questions/22394235/invalid-control-character-with-python-json-loads
OpenAI function call is not a FunctionMessage type
Labels: name: auto:bug, name: area: models

### System Info

I am playing around with tools in LangChain,  but I am running into an issue where the output of the model is not a `FunctionMessage` type even though the LLM is making a function call. For example in this code below
```python

# Import libraries
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage, AIMessage, FunctionMessage
from langchain.tools import tool, format_tool_to_openai_function

# Initialize the chat model
model = ChatOpenAI(model="gpt-3.5-turbo-0613", temperature=0)

# Define tools
@tool
def myfunc(x):
    """Calculate myfunc(x)."""
    return x ** 0.5
@tool
def myotherfunc(x):
    """Calculate myotherfunc(x)."""
    return x ** 2
tools = [myfunc]

# Define your messages
messages = [
    SystemMessage(content="You are a helpful AI who can calculate special functions using provided functions."),
    HumanMessage(content="What is func(4)?")
]

# Call the predict_messages method
functions = [format_tool_to_openai_function(t) for t in tools]
response = model.predict_messages(messages, functions=functions)

# Print the assistant's reply
print(response)
```
`response` is an AI message
```
AIMessage(content='', additional_kwargs={'function_call': {'name': 'myfunc', 'arguments': '{\n  "x": 4\n}'}}, example=False)
```
But I believe it should be a FunctionMessage.

Secondly, there is no clear way to turn an `AIMessage` of this type into a FunctionMessage to pass downstream. I have tried
```python
FunctionMessage(content='', **response.additional_kwargs)
FunctionMessage(content='', additional_kwargs=response.additional_kwargs)
FunctionMessage(content='', additional_kwargs=str(response.additional_kwargs))
```
but each of these attempts gives an error.

### Who can help?

_No response_

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

### Reproduction

Run the code provided above.

### Expected behavior

The output should be a `FunctionMessage`.
Rely on `WORKDIR` env var to avoid ugly ternary operators in workflows.
Labels: name: auto:improvement

Ternary operators in GitHub Actions syntax are pretty ugly and hard to read: `inputs.working-directory == '' && '.' || inputs.working-directory` means "if the condition is true, use `'.'` and otherwise use the expression after the `||`".

This PR performs the ternary as few times as possible, assigning its outcome to an env var we can then reuse as needed.
cannot import name 'AzureOpenAI' from 'langchain.llms'
Labels: name: area: embeddings, name: auto:bug, name: area: models

### System Info

I got this error from on my office laptop
OS: Win 10

I checked AzureAI key, url, deployment and model names. No problem about that.
This guy having same issue with me : https://stackoverflow.com/questions/76750207/azureopenai-not-available-in-langchain

I deleted "import openai" part but no change.

Could somebody pls help me?

```
from dotenv import load_dotenv
from langchain.llms import AzureOpenAI
from langchain.embeddings import OpenAIEmbeddings
import openai
import os


# Load environment variables
load_dotenv()

# Configure Azure OpenAI Service API
openai.api_type = "azure"
openai.api_version = "2022-12-01"
openai.api_base = os.getenv('OPENAI_API_BASE')
openai.api_key = os.getenv("OPENAI_API_KEY")

# Create a completion
llm = AzureOpenAI(deployment_name="text-davinci-003", model_name="text-davinci-003")
joke = llm("Tell me a dad joke")
print(joke)
```

### 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
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

from dotenv import load_dotenv
from langchain.llms import AzureOpenAI
from langchain.embeddings import OpenAIEmbeddings
import openai
import os


# Load environment variables
load_dotenv()

# Configure Azure OpenAI Service API
openai.api_type = "azure"
openai.api_version = "2022-12-01"
openai.api_base = os.getenv('OPENAI_API_BASE')
openai.api_key = os.getenv("OPENAI_API_KEY")

### Expected behavior

from dotenv import load_dotenv
from langchain.llms import AzureOpenAI
from langchain.embeddings import OpenAIEmbeddings
import openai
import os
Issue 9401 - SequentialChain runs the same callbacks over and over in async mode
Labels: name: lgtm, name: auto:bug

Issue: https://github.com/langchain-ai/langchain/issues/9401

In the Async mode, SequentialChain implementation seems to run the same callbacks over and over since it is re-using the same callbacks object.

Langchain version: 0.0.264, master

The implementation of this aysnc route differs from the sync route and sync approach follows the right pattern of generating a new callbacks object instead of re-using the old one and thus avoiding the cascading run of callbacks at each step.

Async mode:
```
        _run_manager = run_manager or AsyncCallbackManagerForChainRun.get_noop_manager()
        callbacks = _run_manager.get_child()
        ...
        for i, chain in enumerate(self.chains):
            _input = await chain.arun(_input, callbacks=callbacks)
            ...
```

Regular mode:
```
        _run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager()
        for i, chain in enumerate(self.chains):
            _input = chain.run(_input, callbacks=_run_manager.get_child(f"step_{i+1}"))
            ...
```

Notice how we are reusing the callbacks object in the Async code which will have a cascading effect as we run through the chain. It runs the same callbacks over and over resulting in issues.

Solution:
Define the async function in the same pattern as the regular one and added tests.

<!-- 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.
 -->
Aksing about uploaded documents only works when I first ask the AI on a specific one
Labels: name: area: agent, name: auto:question, name: area: models

### System Info

Hello!
I wrote a code that is very similar to this one
https://python.langchain.com/docs/integrations/toolkits/document_comparison_toolkit



### Who can help?

_No response_

### Information

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

### Related Components

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

### Reproduction

    class DocumentInput(BaseModel):
        question: str = Field()
    
    
    llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
    
    tools = []
    files = [
        # https://abc.xyz/investor/static/pdf/2023Q1_alphabet_earnings_release.pdf
        {
            "name": "alphabet-earnings",
            "path": "/Users/harrisonchase/Downloads/2023Q1_alphabet_earnings_release.pdf",
        },
        # https://digitalassets.tesla.com/tesla-contents/image/upload/IR/TSLA-Q1-2023-Update
        {
            "name": "tesla-earnings",
            "path": "/Users/harrisonchase/Downloads/TSLA-Q1-2023-Update.pdf",
        },
    ]
    
    for file in files:
        loader = PyPDFLoader(file["path"])
        pages = loader.load_and_split()
        text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
        docs = text_splitter.split_documents(pages)
        embeddings = OpenAIEmbeddings()
        retriever = FAISS.from_documents(docs, embeddings).as_retriever()
            # Wrap retrievers in a Tool
    tools.append(
        Tool(
            args_schema=DocumentInput,
            name=file["name"],
            description=f"useful when you want to answer questions about {file['name']}",
            func=RetrievalQA.from_chain_type(llm=llm, retriever=retriever),
        )
    )
    llm = ChatOpenAI(
    temperature=0,
    model="gpt-3.5-turbo-0613",
    )
    
    agent = initialize_agent(
        agent=AgentType.OPENAI_FUNCTIONS,
        tools=tools,
        llm=llm,
        verbose=True,
    )
    
    agent({"input": "did alphabet or tesla have more revenue?"})

### Expected behavior

It's working so far, but when I directly ask the AI to compare the documents mentioning them by name, 
the AI does not know what I am talking about!
I first have to ask questions specificly about each document and seperatly mentioning their names, then the llm can compare them and answer my questions.
Any idea to solve this issue?

I would appreaciate any help!:)

cheers
Add some control over the summary length for summarize chains
Labels: name: auto:improvement, name: area: models

### Feature request

For the chains in libs/langchain/langchain/chains/summarize, it might be useful to add to the prompt something like: 

"Try to limit the summary to {length} words", and set the length as an input parameter. So that we can have some influence over the length of the output summary.

I know that most LLMs are not good at counting words, maybe we should use token instead of words?

### Motivation

When doing summarization, we sometimes want summaries of different length. For example, we might fit the summary into some system with length limit, or sometimes we want a longer summary so we can include more details. 

### Your contribution

I can submit a pull request, but I'm not sure if people believe the proposal is a good idea or not.
docs: Add memgraph notebook
Labels: name: lgtm, name: area: doc loader, name: auto:documentation

  - Description: added graph_memgraph_qa.ipynb which shows how to use LLMs to provide a natural language interface to a Memgraph database using [MemgraphGraph](https://github.com/langchain-ai/langchain/pull/8591) class.
  - Dependencies: given that the notebook utilizes the MemgraphGraph class, it relies on both this class and several Python packages that are installed in the notebook using pip (langchain, openai, neo4j, gqlalchemy). The notebook is dependent on having a functional Memgraph instance running, as it requires this instance to establish a connection.
Add lock on tee peer cleanup
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.
 -->
Runnables: Add .map() method
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.
 -->
Runnables: Use a shared executor for all parallel calls (sync)
Labels: name: auto:improvement

Async equivalent coming in future PR

<!-- 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.
 -->