all Codit insights

A Simple Chat App with LangChain

In the final blog post of this series, we will have a look at how to make an AI-driven app in LangChain.

LangChain is one of three major frameworks that make creating AI apps easier, the other two being Prompt Flow and Semantic Kernel. A basic understanding of python is required to start using LangChain.

Developing a simple chat application with LangChain requires minimal effort. You can have your application up and running in a few lines of code.

Requirements: A deployed LLM in Azure.

LLM Object 

All you need for this is a .env file and a single empty python file. Our first step is to establish a connection with the AI model deployed in Azure by creating an LLM object. This connection requires the deployment name, the model’s name, your endpoint, and API key. You should also define the maximum number of tokens and the temperature.

Template

Once you have created your LLM object, you can design the template for your prompt. This prompt will be the system role for the AI. It will decide the personality and specialty of the AI. This can be done by creating a simple string which is used later. Ensure that the question can be input into the template.

Creating Chain

Subsequently, you can construct your initial chain. This example demonstrates the creation of a simple chain that accepts a string as a prompt. There are multiple ways to create a chain, including using the prompt template class. The method  shown here is the most straightforward.

Chains: https://python.langchain.com/v0.1/docs/modules/chains/

Invoke the Chain

With the necessary setup complete, all that remains is to ask the question and initiate the chain. When invoking the chain, pass in the question and await the response, which you can then print.

This process completes the creation of a basic AI chat application. As you become more familiar with the basics, you can begin to enhance your application by adding advanced features.

Complete Code

With this code, you can ask a single question to the AI in the terminal. With a well-placed loop, you can continue asking questions.

Conclusion

For a simple app like this, LangChain is perfect. All you need is a simple python file, and then you can write the code and start the script.

Thanks for reading!

Subscribe to our RSS feed

Want to know more?

Contact Steven

IoT Data & AI Domain Lead - Data & AI Solution Architect

Hi there,
how can we help?

Got a project in mind?

Connect with us

Let's talk

Let's talk

Thanks, we'll be in touch soon!

Call us

Thanks, we've sent the link to your inbox

Invalid email address

Submit

Your download should start shortly!

Stay in Touch - Subscribe to Our Newsletter

Keep up to date with industry trends, events and the latest customer stories

Invalid email address

Submit

Great you’re on the list!