cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
544
Views
2
Helpful
1
Replies

LangChain - A Cool Python Framework for LLM Applications

davidn#
Cisco Employee
Cisco Employee

langchain.jpegThe world of technology is constantly evolving, and large language models (LLMs) have emerged as groundbreaking tools that are revolutionizing the way we interact with applications and digital content. These sophisticated language models, such as ChatGPT and Google BARD, have opened up new possibilities for engineers, enabling them to create applications that were once beyond reach.

If you’re a Python programmer interested in learning about AI and LLMs, LangChain is an exciting new Python framework that you must try. LangChain simplifies the process of developing applications powered by language models, providing flexible abstractions and an AI-first toolkit that unlocks developers to build context-aware, reasoning LLM applications. LangChain’s tools and APIs simplify the process of building LLM-driven applications like chatbots and virtual agents.

To install LangChain run:

 

pip install langchain

 

To try out this simple example you also need to install dotenv and openai modules:

 

pip install openai
pip install python-dotenv

 

On MacOS or Linux, you may want to replace pip with pip3. Also, create a virtual environment before installing these modules. You will need to create an OpenAI account‍ If you don't already have an OpenAI account and generate an API key before try out this example.

 

from langchain_openai import ChatOpenAI
from langchain.prompts.chat import ChatPromptTemplate
from dotenv import load_dotenv
import os

load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
chat_model = ChatOpenAI(openai_api_key=api_key)

template = "You are a helpful assistant that translates {input_language} to {output_language}."
human_template = "{text}"

chat_prompt = ChatPromptTemplate.from_messages([
    ("system", template),
    ("human", human_template),
])

messages = chat_prompt.format_messages(input_language="English", output_language="Spanish", 
                                                                     text="I love Devvie.")
result = chat_model.predict_messages(messages)
print(result.content)

 

Have fun with LangChain!

1 Reply 1

Ruben Cocheno
Spotlight
Spotlight

@davidn# 

All sorted?

Tag me to follow up.
Please mark it as Helpful and/or Solution Accepted if that is the case. Thanks for making Engineering easy again.
Connect with me for more on Linkedin https://www.linkedin.com/in/rubencocheno/