Question Answering with LangChain and OpenAI

This interactive notebook uses Langchain to split fictional workplace documents into passages and uses OpenAI to transform these passages into embeddings and store them into Elasticsearch.

Then when we ask a question, we retrieve the relevant passages from the vector store and use langchain and OpenAI to provide a summary for the question.

Install required packages

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. langserve 0.0.21 requires pydantic<2,>=1, but you have pydantic 2.3.0 which is incompatible.  [notice] A new release of pip is available: 23.2 -> 23.3.1 [notice] To update, run: pip install --upgrade pip

Connect to Elasticsearch

ℹ️ We're using an Elastic Cloud deployment of Elasticsearch for this notebook. If you don't have an Elastic Cloud deployment, sign up here for a free trial.

We'll use the Cloud ID to identify our deployment, because we are using Elastic Cloud deployment. To find the Cloud ID for your deployment, go to https://cloud.elastic.co/deployments and select your deployment.

We will use ElasticsearchStore to connect to our elastic cloud deployment. This would help create and index data easily. In the ElasticsearchStore instance, will set embedding to OpenAIEmbeddings to embed the texts and elasticsearch index name that will be used in this example.

Indexing Data into Elasticsearch

Let's download the sample dataset and deserialize the document.

Split Documents into Passages

We’ll chunk documents into passages in order to improve the retrieval specificity and to ensure that we can provide multiple passages within the context window of the final question answering prompt.

Here we are chunking documents into 800 token passages with an overlap of 400 tokens.

Here we are using a simple splitter but Langchain offers more advanced splitters to reduce the chance of context being lost.

Bulk Import Passages

Now that we have split each document into the chunk size of 800, we will now index data to elasticsearch using ElasticsearchStore.from_documents.

We will use Cloud ID, Password and Index name values set in the Create cloud deployment step.

Asking a question

Now that we have the passages stored in Elasticsearch, we can now ask a question to get the relevant passages.

---- Answer ---- The NASA Sales Team is responsible for understanding the unique market dynamics and cultural nuances of North and South America. It is led by Area Vice-Presidents Laura Martinez (North America) and Gary Johnson (South America), and consists of dedicated account managers, sales representatives, and support staff. The team works to effectively target and engage with customers across the region.

Add Source Tracing

RAG can provide clear traceability of the source knowledge used to answer a question. This is important for compliance and regulatory reasons and limiting LLM hallucinations. This is known as source tracking.

In this example, we extend the Prompt template to ask the LLM to cite the source of the answer.

---- Answer ---- The North America South America (NASA) sales team is responsible for serving customers and achieving business objectives across North and South America. The team is led by two Area Vice-Presidents: Laura Martinez is the Area Vice-President of North America, and Gary Johnson is the Area Vice-President of South America. The team consists of dedicated account managers, sales representatives, and support staff. They are responsible for identifying and pursuing new business opportunities, nurturing existing client relationships, and ensuring customer satisfaction. SOURCE: Sales Organization Overview

Returning Passages with Answer

In this example, we extend the chain to return the passages back with the answer. This is helpful for the UI to display the source passages, should the user want to read more on the topic.

---- Answer ---- The North America South America (NASA) region has two Area Vice-Presidents: Laura Martinez is the Area Vice-President of North America, and Gary Johnson is the Area Vice-President of South America. The NASA sales team consists of dedicated account managers, sales representatives, and support staff, led by their respective Area Vice-Presidents. They are responsible for identifying and pursuing new business opportunities, nurturing existing client relationships, and ensuring customer satisfaction. The teams collaborate closely with other departments, such as marketing, product development, and customer support, to ensure we consistently deliver high-quality products and services to our clients. SOURCE: Sales Organization Overview ---- Documents ---- Sales Organization Overview Our sales organization is structured to effectively serve our customers and achieve our business objectives across multiple regions. The organization is divided into the following main regions: The Americas: This region includes the United States, Canada, Mexico, as well as Central and South America. The North America South America region (NASA) has two Area Vice-Presidents: Laura Martinez is the Area Vice-President of North America, and Gary Johnson is the Area Vice-President of South America. Europe: Our European sales team covers the entire continent, including the United Kingdom, Germany, France, Spain, Italy, and other countries. The team is responsible for understanding the unique market dynamics and cultural nuances, enabling them to effectively target and engage with customers across the region. The Area Vice-President for Europe is Rajesh Patel. Asia-Pacific: This region encompasses countries such as China, Japan, South Korea, India, Australia, and New Zealand. Our sales team in the Asia-Pacific region works diligently to capitalize on growth opportunities and address the diverse needs of customers in this vast and rapidly evolving market. The Area Vice-President for Asia-Pacific is Mei Li. Middle East & Africa: This region comprises countries across the Middle East and Africa, such as the United Arab Emirates, Saudi Arabia, South Africa, and Nigeria. Our sales team in this region is responsible for navigating the unique market challenges and identifying opportunities to expand our presence and better serve our customers. The Area Vice-President for Middle East & Africa is Jamal Abdi. Each regional sales team consists of dedicated account managers, sales representatives, and support staff, led by their respective Area Vice-Presidents. They are responsible for identifying and pursuing new business opportunities, nurturing existing client relationships, and ensuring customer satisfaction. The teams collaborate closely with other departments, such as marketing, product development, and customer support, to ensure we consistently deliver high-quality products and services to our clients. ---- Sales Engineering Collaboration As an engineer, it is important to understand the sales team's goals and objectives, as this will help you to provide them with the necessary information, tools, and support to successfully sell your company's products and services. Communication: Effective communication is key to successfully working with the sales team. Make sure to maintain open lines of communication, and be responsive to their questions and concerns. This includes: a. Attending sales meetings and conference calls when required. b. Providing regular product updates and training sessions to the sales team. c. Being available to answer technical questions and clarifications. Collaboration: Collaborate with the sales team in developing and refining sales materials, such as product presentations, demos, and technical documents. This will ensure that the sales team has accurate and up-to-date information to present to clients. Additionally, work closely with the sales team on customer projects or product customizations, providing technical guidance, and ensuring that the solutions meet the customer's requirements. Customer Engagement: At times, engineers may be asked to join sales meetings or calls with potential clients to provide technical expertise. In these situations, it is important to: a. Be prepared and understand the customer's needs and pain points. b. Clearly explain the technical aspects of the product or solution in a simple language that the customer can understand. c. Address any concerns or questions the customer may have. Continuous Improvement: Actively seek feedback from the sales team regarding product performance, customer experiences, and market trends. Use this feedback to identify areas of improvement and collaborate with other engineers to enhance the product or service offerings. Mutual Respect and Support: It is essential to treat your colleagues in the sales team with respect and professionalism. Recognize and appreciate their efforts in promoting and selling the company's products and services. In turn, the sales team should also respect and appreciate the technical expertise and knowledge of the engineering team. By working together, both the engineering and sales teams can contribute to the overall success of the company. Conclusion: Collaboration between engineers and the sales team is crucial for a tech company's success. By understanding each other's roles, maintaining effective communication, collaborating on projects, and supporting one another, both teams can work together to achieve the company's goals and ensure customer satisfaction. ---- Fy2024 Company Sales Strategy III. Action Plans A. Sales Team Development: Expand the sales team to cover new markets and industries. Provide ongoing training to sales staff on product knowledge, sales techniques, and industry trends. Implement a performance-based incentive system to reward top performers. B. Marketing and Promotion: Develop targeted marketing campaigns for different customer segments and industries. Leverage digital marketing channels to increase brand visibility and lead generation. Participate in industry events and trade shows to showcase our products and services. C. Partner Ecosystem: Strengthen existing partnerships and establish new strategic alliances to expand market reach. Collaborate with partners on joint marketing and sales initiatives. Provide partner training and support to ensure they effectively represent our products and services. D. Customer Success: Implement a proactive customer success program to improve customer retention and satisfaction. Develop a dedicated customer support team to address customer inquiries and concerns promptly. Collect and analyze customer feedback to identify areas for improvement in our products, services, and processes. IV. Monitoring and Evaluation Establish key performance indicators (KPIs) to track progress toward our objectives. Conduct regular sales team meetings to review performance, share best practices, and address challenges. Conduct quarterly reviews of our sales strategy to ensure alignment with market trends and adjust as needed. By following this sales strategy for fiscal year 2024, our tech company aims to achieve significant growth and success in our target markets, while also providing exceptional value and service to our customers. ---- Sales Engineering Collaboration Title: Working with the Sales Team as an Engineer in a Tech Company Introduction: As an engineer in a tech company, collaboration with the sales team is essential to ensure the success of the company's products and services. This guidance document aims to provide an overview of how engineers can effectively work with the sales team, fostering a positive and productive working environment. Understanding the Sales Team's Role: The sales team is responsible for promoting and selling the company's products and services to potential clients. Their role involves establishing relationships with customers, understanding their needs, and ensuring that the offered solutions align with their requirements. As an engineer, it is important to understand the sales team's goals and objectives, as this will help you to provide them with the necessary information, tools, and support to successfully sell your company's products and services. Communication: Effective communication is key to successfully working with the sales team. Make sure to maintain open lines of communication, and be responsive to their questions and concerns. This includes: a. Attending sales meetings and conference calls when required. b. Providing regular product updates and training sessions to the sales team. c. Being available to answer technical questions and clarifications. Collaboration: Collaborate with the sales team in developing and refining sales materials, such as product presentations, demos, and technical documents. This will ensure that the sales team has accurate and up-to-date information to present to clients. Additionally, work closely with the sales team on customer projects or product customizations, providing technical guidance, and ensuring that the solutions meet the customer's requirements. Customer Engagement: At times, engineers may be asked to join sales meetings or calls with potential clients to provide technical expertise. In these situations, it is important to: ----

Conversational Question Answering

We have achieved getting answers to questions, but what if we want to ask follow up questions? We can use the answer from the previous question as the context for the next question. This is known as conversational question answering.

In this example, we extend the chain to use the answer from the previous question as the context for the next question.

---- Answer ---- The objectives for fiscal year 2024 are to increase revenue by 20% compared to fiscal year 2023, expand market share in key segments by 15%, retain 95% of existing customers and increase customer satisfaction ratings, and launch at least two new products or services in high-demand market segments. SOURCE: Fy2024 Company Sales Strategy

Next Steps

We have shown how to use Langchain to build a question answering system. We have shown how to index data into Elasticsearch, ask a question and use the answer from the previous question as the context for the next question.

Ready to build state of the art search experiences?

Sufficiently advanced search isn’t achieved with the efforts of one. Elasticsearch is powered by data scientists, ML ops, engineers, and many more who are just as passionate about search as you are. Let’s connect and work together to build the magical search experience that will get you the results you want.

Try it yourself