mteb/FreshStackRetrieval huggingface dataset:
I am working on a langchain based SQL chat application and wanted my agent to understand context w.r.t the user session. For e.g.
User - What is highest order placed in last placed?
Bot - Order id : XYZ
User - When was this placed?
Here, bot should be able to deduce that 'this' refers to 'order id XYZ' from previous question. How can I incorporate this in my code?
I am tried using ChatHistory but getting context from session history is where I am stuck.
I'm going to learn LangChain and stumble upon their Getting Started section. Because it doesn't work and I'm curious if I am the only person where LangChain examples don't work.
This is their tutorial I am talking about. https://python.langchain.com/docs/get_started/quickstart/
Let's use the very first example:
llm = ChatOpenAI(openai_api_key=api_key)
llm.invoke("how can langsmith help with testing?")

I wrote some initializing code as well to make ChatOpenAI work:
import os
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv

load_dotenv()

api_key = os.getenv("OPENAI_API_KEY")

llm = ChatOpenAI(openai_api_key=api_key)
llm.invoke("how can langsmith help with testing?")

The invoke function seems to be executed as I can't see any error message. But I also can't see any further output. Nothing happens.
They even wrote "We can also guide its response with a prompt template.". However,  there is not response.
Who can explain to me, what is happening here? And can you probably recommend me a better tutorial instead of that from LangChain?
I have a database in which I have connected an agent too. However, I have noticed that it sometimes gets confused between whether or not it should return a column ID or persons first name when asked "which person sold the most....?" Is there a way to tune/adjust the create_sql_agent from langchain.agents at which I can tell the agent to not return column ID but return first and last name based on questions structured like that?
I think the question may be related to this post but I am unsure how to include that/and structure that properly: https://github.com/langchain-ai/langchain/discussions/9591
System Info
langchain-openai==0.1.3
Python 3.11.7
Windows 11
Basic Model
from langchain_openai import ChatOpenAI
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.agents import create_sql_agent
from langchain.sql_database import SQLDatabase


llm = ChatOpenAI(model_name="gpt-3.5-turbo-1106", temperature=0, openai_api_key=os.environ.get('OPENAI_API_KEY'))

toolkit = SQLDatabaseToolkit(db=db, llm=llm)

agent_executor = create_sql_agent(
    llm=llm,
    toolkit=toolkit,
    verbose=False,
    agent_type="openai-tools")


print(agent_executor.invoke("What is my data about"))

Nothing, unsure how to progress as I can not find examples.
Currently, I am getting back multiple responses, or the model doesn't know when to end a response, and it seems to repeat the system prompt in the response(?). I simply want to get a single response back. My setup is very simple, so I imagine I am missing implementation details, but what can I do to only return the single response?
from langchain_community.llms import Ollama

llm = Ollama(model="llama3")

def get_model_response(user_prompt, system_prompt):
    prompt = f"""
        

        
system

        { system_prompt }
        

        
user

        { user_prompt }
        

        
assistant

        """
    response = llm.invoke(prompt)
    return response
I'm working with the langchain library to implement a document analysis application.  Especifically I want to use the routing technique described in this documentation. i wanted to follow along the example, but my environment is restricted to AWS, and I am using ChatBedrock instead of ChatOpenAI due to limitations with my deployment.
According to this overview the with_structured_output method, which I need, is not (yet) implemented for models on AWS Bedrock, which is why I am looking for a workaround or any method to replicate this functionality.
The key functionality I am looking for is shown in this example:
from typing import List
from typing import Literal

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI



class RouteQuery(BaseModel):
    """Route a user query to the most relevant datasource."""

    datasources: List[Literal["python_docs", "js_docs", "golang_docs"]] = Field(
        ...,
        description="Given a user question choose which datasources would be most relevant for answering their question",
    )

system = """You are an expert at routing a user question to the appropriate data source.

Based on the programming language the question is referring to, route it to the relevant data source."""
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system),
        ("human", "{question}"),
    ]
)

llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(RouteQuery)
router = prompt | structured_llm
router.invoke(
    {
        "question": "is there feature parity between the Python and JS implementations of OpenAI chat models"
    }
)

The output would be:
RouteQuery(datasources=['python_docs', 'js_docs'])

The most important fact for me is that it just selects items from the list without any additional overhead, which makes it possible to setup the right follow up questions.
Did anyone find a workaround how to resolve this issue?
I am experimenting with a langchain chain by passing multiple arguments.
Here is a scenario:
TEMPLATE = """Task: Generate Cypher statement to query a graph database.
Instructions:
Use only the provided relationship types and properties in the schema.
Do not use any other relationship types or properties that are not provided.

You are also provided with contexts to generate cypher queries. These contexts are the node ids from the Schema.
{schema}

Some examples of contexts:
{context}

The question is:
{question}"""

prompt = PromptTemplate.from_template(template=TEMPLATE)
chain = (
    {
     "schema": schema,
     "context": vector_retriever_chain | extract_relevant_docs, 
     "question": RunnablePassthrough()
    }
    | prompt
)
chain.invoke("my question?")

In this chain, I am getting some context from a vector retriever which I am passing to a function called extract_relevant_docs() that will parse the result and get format I want.
The tricky part here is the variable 'schema' which I also want to supply to design my prompt. How can I pass these variables during the chain.invoke().
Thank you
I'm working with LangChain's Chroma VectorStore, and I'm trying to filter documents based on a list of document names.
I have a list of document names as follows:
lst = ['doc1', 'doc2', 'doc3']

I also have doc_name metadata in my VectorStore. Currently, I’m using the following code to retrieve documents:
base_retriever = chroma_db.as_retriever(search_kwargs={'k': 10})

However, I’m not sure how to modify this code to filter documents based on my list of document names. Could anyone guide me on how to achieve this? Any help would be greatly appreciated!
I have a simple series of nodes that are chained together with conditional edges.  The first two are shown here:
def email_router(state: TypedDict) -> None:
    node_results, state = get_emails(state)
    if node_results == "New Email":
        state["internal_state"] = "topics_router"
    else:
        state["errors"] = "No New Mail"
        state["internal_state"] = "return_final_status"

def check_internal_state(state: TypedDict) -> str:
    logging.debug(f"Internal State: {state["internal_state"]}")
    return state["internal_state"]

In the email router, I am setting the state value "internal_state" to either "topics_router" or "return_final_status" which are two other nodes.  The check_internal_state is there simply to enable the conditional edge which looks as follows:
workflow.add_conditional_edges(
    "email_router",
    check_internal_state,
    {
        "topics_router": "topics_router",
        "return_final_status": "return_final_status",
    },
)

The logging debug statement in the check_internal_state keeps returning an empty string which means that I am not properly carrying the state information across and I cannot figure out what I am doing wrong.
The code is set in classes. the one function compiling the code and saving the output as:
global mailman
mailman = workflow.compile()

I have another function, in the same python file, then invoking mailman and calling functions that all reside in another python file but that are imported.  I only bring this up as maybe separating things into multiple functions is causing the issue?  Here is the overall workflow:
workflow = StateGraph(GraphState)
workflow.add_node("email_router", email_router)
workflow.add_node("topics_router", topics_router)
workflow.add_node("status_router", status_router)
workflow.add_node("actions_router", actions_router)
workflow.add_node("return_final_status", return_final_status_node)

workflow.set_entry_point("email_router")

workflow.add_conditional_edges(
    "email_router",
    check_internal_state,
    {
        "topics_router": "topics_router",
        "return_final_status": "return_final_status",
    },
)

workflow.add_conditional_edges(
    "topics_router",
    check_internal_state,
    {
        "status_router": "status_router",
        "return_final_status": "return_final_status",
    },
)

workflow.add_conditional_edges(
    "status_router",
    check_internal_state,
    {
        "actions_router": "actions_router",
        "return_final_status": "return_final_status",
    },
)

workflow.add_edge("actions_router", "return_final_status")
workflow.add_edge("return_final_status", END)

Does anybody know what I am doing wrong?  Thank you!
I am struggeling with basic chaining and passing input parameters through RunnableSequences in LangChain v0.2.
I have two chains: code_chain and test_chain.


Now I want to chain them together.

This is my current code:
code_prompt = PromptTemplate.from_template("Write a very short {language} function that will {task}");
code_chain = code_prompt | llm | {"code": StrOutputParser()};

test_prompt = PromptTemplate.from_template("Write a test for the following {language} code:\n{code}");
test_chain = test_prompt | llm | {"test": StrOutputParser()};

chain = code_chain | test_chain;

result = chain.invoke({
    "language": "python",
    "task": "reverse a string",
});

Because code_chain does not retain the language parameter as an output, it is missing in the test_chain:
KeyError: "Input to PromptTemplate is missing variables {'language'}.  Expected: ['code', 'language'] Received: ['code']"
How do I pass the language input of the first chain to the language input of the second?
I'm trying to create a vector DB which will be populated with embeddings of articles from my employer's blog.
I've got a Milvus instance up and running and am able to follow the walkthrough on the Langchain website.
Based on the walkthrough, my implementation so far looks something like this:
def parseWPDataFile(filename):
    # redacted for brevity
    return {
        'meta': parsed_headers,
        'body': doc_body.strip()
    }

parsed_doc = parseWPDataFile('sample_data.txt')
text_splitter = RecursiveCharacterTextSplitter(is_separator_regex=True, separators=['\n+'], chunk_size=5000, length_function=len)
docs = text_splitter.create_documents([parsed_doc['body']], [parsed_doc['meta']])
embeddings = OpenAIEmbeddings()
vector_db = Milvus.from_documents(docs, embeddings, connection_args={"host": "127.0.0.1", "port": "19530"})

This being my first time using a vector database, I'm a little confused by that last line. The documentation for Milvus.from_documents indicates that it creates a vectorstore from documents, I guess, in memory. What I want is a persistent vectorstore that I can load stuff into and then later, in a separate script, pull from. I can't find any Langchain examples of this.
How do I create a persistent VectorStore, add to it, and get a reference to it later, in another script?
I do not understand why the below use of the PydanticOutputParser is erroring.
The docs do not seem correct - If I follow this exactly (i.e. use with_structured_output exclusively, without an output parser) then the output is a dict, not Pydantic class. So I thought I modified it consistently with so SO answers e.g. this
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain.output_parsers import PydanticOutputParser

from uuid import uuid4
from pydantic import BaseModel, Field

class TestSummary(BaseModel):
    """Represents a summary of the concept"""

    id: str = Field(default_factory=lambda: str(uuid4()), description="Unique identifier")
    summary: str = Field(description="Succinct summary")
 
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0).with_structured_output(TestSummary)
parser = PydanticOutputParser(pydantic_object=TestSummary)
prompt = PromptTemplate(
    template="You are an AI summarizing long texts. TEXT: {stmt}",
    input_variables=["stmt"]
)
runnable = prompt | llm | parser 
result = runnable.invoke({"stmt": "This is a really long piece of literature I'm too lazy to read"})

The error is
ValidationError: 1 validation error for Generation
text
  str type expected (type=type_error.str)

As discussed, if I omit the output parser, I get a dict:
runnable = prompt | llm #| parser 
result = runnable.invoke({"stmt": "This is a really long piece of literature I'm too lazy to read"})
type(result)
dict
I have a document with three attributes: tags, location, and text.
Currently, I am indexing all of them using LangChain/pgvector/embeddings.
I have satisfactory results, but I want to know if there is a better way since I want to find one or more documents with a specific tag and location, but the text can vary drastically while still meaning the same thing. I thought about using embeddings/vector databases for this reason.
Would it also be a case of using RAG (Retrieval-Augmented Generation) to "teach" the LLM about some common abbreviations that it doesn't know?
import pandas as pd

from langchain_core.documents import Document
from langchain_postgres import PGVector
from langchain_postgres.vectorstores import PGVector
from langchain_openai.embeddings import OpenAIEmbeddings

connection = "postgresql+psycopg://langchain:langchain@localhost:5432/langchain"
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
collection_name = "notas_v0"

vectorstore = PGVector(
    embeddings=embeddings,
    collection_name=collection_name,
    connection=connection,
    use_jsonb=True,
)


# START INDEX

# df = pd.read_csv("notes.csv")
# df = df.dropna()  # .head(10000)
# df["tags"] = df["tags"].apply(
#     lambda x: [tag.strip() for tag in x.split(",") if tag.strip()]
# )


# long_texts = df["Texto Longo"].tolist()
# wc = df["Centro Trabalho Responsável"].tolist()
# notes = df["Nota"].tolist()
# tags = df["tags"].tolist()

# documents = list(
#     map(
#         lambda x: Document(
#             page_content=x[0], metadata={"wc": x[1], "note": x[2], "tags": x[3]}
#         ),
#         zip(long_texts, wc, notes, tags),
#     )
# )

# print(
#     [
#         vectorstore.add_documents(documents=documents[i : i + 100])
#         for i in range(0, len(documents), 100)
#     ]
# )
# print("Done.")

### END INDEX

### BEGIN QUERY

result = vectorstore.similarity_search_with_relevance_scores(
    "EVTD202301222707",
    filter={"note": {"$in": ["15310116"]}, "tags": {"$in": ["abcd", "xyz"]}},
    k=10, # Limit of results
)

### END QUERY
So far my research only shows me how to filter to a specific a specific document or page but it doesn't show how to exclude some documents from the search.
results_with_scores = db.similarity_search_with_score("foo", filter=dict(page=1))
I have this LangChain code for answering questions by getting similar docs from the vector store and using llm to get the answer of the query:
llm_4 = AzureOpenAI(
        # temperature=0,
        api_version= os.environ['OPENAI_API_VERSION_4'], 
        openai_api_key= os.environ['AZURE_OPENAI_API_KEY_4'], 

        deployment_name="gpt4-deploy",
        # model_name="gpt4-o",
        azure_endpoint=os.environ['AZURE_OPENAI_ENDPOINT_4']
    )

    llm_3 = AzureOpenAI(
        # temperature=0,
        api_version= os.environ['OPENAI_API_VERSION_3'], 
        openai_api_key= os.environ['AZURE_OPENAI_API_KEY_3'], 

        deployment_name="test-deployment", 
        # deployment_name="gpt-16k-deployment",
        # model_name="gpt-3.5-turbo-16k",

        azure_endpoint=os.environ['AZURE_OPENAI_ENDPOINT_3']
    )

    response=get_answer(relavant_docs, user_input, llm_4)

...
#Create embeddings instance
def create_embeddings():
    #embeddings = OpenAIEmbeddings()
    embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
    # embeddings = SentenceTransformerEmbeddings(model_name="text-davinci-003") 
    return embeddings


def get_answer(docs, user_input, llm=None):
    if llm:
        chain = load_qa_chain(llm, chain_type="stuff")
    else:
        chain = load_qa_chain(OpenAI(), chain_type="stuff")

    with get_openai_callback() as cb:
        response = chain.run(input_documents=docs, question=user_input)
    return response

It's working with gpt3, but with gpt4 getting:

BadRequestError: Error code: 400 - {'error': {'code': 'OperationNotSupported', 'message': 'The completion operation does not work with the specified model, gpt-4. Please choose different model and try again. You can learn more about which models can be used with each operation here: https://go.microsoft.com/fwlink/?linkid=2197993.'}}

I tried what was suggested by these similar issues:
How to use the new gpt-3.5-16k model with langchain?
I am trying to make a docs question answering program with AzureOpenAI and Langchain
But I still didn't figure out how to solve it!
I'm working on integrating LangChain with AzureOpenAI in Python and encountering a couple of issues. I've recently updated from a deprecated method to a new class implementation, but now I'm stuck with some errors I don't fully understand. Here's the relevant part of my code:
from langchain_openai import AzureOpenAI as LCAzureOpenAI
# from langchain.llms import AzureOpenAI <-- Deprecated

# Create client accessing LangChain's class
client = LCAzureOpenAI(
    openai_api_version=api_version,
    azure_deployment=deployment_name,
    azure_endpoint=azure_endpoint,
    temperature=TEMPERATURE,
    max_tokens=MAX_TOKENS,
    model=model
    #,model_kwargs={'azure_openai_api_key': api_key}
)

# Attempt to send a chat message
client.chat("Hi")

This results in the following error:
AttributeError: 'AzureOpenAI' object has no attribute 'chat'

When I replace client.chat("Hi") with client.invoke("Hi"), I get a different error:
BadRequestError: Error code: 400 - {'error': {'code': 'OperationNotSupported', 'message': 'The completion operation does not work with the specified model, gpt-4. Please choose different model and try again. You can learn more about which models can be used with each operation here: https://go.microsoft.com/fwlink/?linkid=2197993.'}}

How can I resolve these errors?
Any guidance or insights into these errors and how to resolve them would be greatly appreciated!
I make some gate like this:
Gate::define('update-post', function  ($user, Post $post) {
    return $user->hasAccess(['update-post']) or $user->id == $post->user_id;
});

I checked my database and it has update-post access and the user id is same as in the post. but I got:

This action is unauthorized.

errors. so am I do some mistake here? thanks.
I'm trying to store image in 3 different folder inside public folder now I'm able to store in two different folder but when I added 3rd folder path but it was not coping in 3rd folder help me to solve this.
my two folder copying Working code
$input['student_photo'] = time().'.'.$request->student_photo->getClientOriginalExtension();
    $folder1 = public_path('public/path1/');
    $path1 = $folder1 . $input['student_photo']; // path 1
    $request->student_photo->move($folder1, $input['student_photo']); // image saved in first folder
    $path2 = public_path('public/path2/') . $input['student_photo']; // path 2
    \File::copy($path1, $path2);

I tired this code for copy 3rd folder but not working
$input['student_photo'] = time().'.'.$request->student_photo->getClientOriginalExtension();
    $folder1 = public_path('public/path1/');
    $path1 = $folder1 . $input['student_photo']; // path 1
    $request->student_photo->move($folder1, $input['student_photo']); // image saved in first folder
    $path2 = public_path('public/path2/') . $input['student_photo']; // path 2
    $path3 = public_path('public/path3/') . $input['student_photo']; // path 3
    \File::copy($path1, $path2, $path3);
I am trying to write an installer-type app using Laravel 10. Where the purpose is: if I don't set the database information it will redirect me to the setup database. When I fill up the form and submit, it performs the following task:

Update database-related content in the .env file
Clear cache, config, and finally cache new config using the Artisan call.
Finally migrate the database still using the Artisan call.

Although the .env file content is updated, it still uses the last database-related info that existed before the update. I mean, suppose previously DB name was ab_setup, then I update it to nn_setup, .env file shows the DB name as nn_setup, but the browser responds:

Unknown database ab_setup (Connection: mysql, SQL: select * from information_schema.tables where table_schema = nn_setup and table_name = migrations and table_type = 'BASE TABLE').

I have no idea what's actually wrong. Here is my code:
// Update .env file content
$envContent = [
    'DB_CONNECTION' =>  $request->database_connection,
    'DB_HOST'       =>  $request->database_host,
    'DB_PORT'       =>  $request->database_port,
    'DB_DATABASE'   =>  $request->database_name,
    'DB_USERNAME'   =>  $request->database_username,
    'DB_PASSWORD'   =>  $request->database_password
];

foreach( $envContent as $key => $value ) {
    $this->replace_env_value($key, $value);
}

// Clear Cache and Config & Cache new Config
Artisan::call('cache:clear');
Artisan::call('config:clear');
Artisan::call('config:cache');

// Migrate DB and Seed
Artisan::call('migrate');
Artisan::call('db:seed');

// Create Admin User
$user = new User();
$user->name = $request->admin_name;
$user->email = $request->admin_username;
$user->password = Hash::make($request->admin_password);
$user->save();

Does anyone have the idea what I miss that will fix the issue?