Unleashing the Potential of Gemini Pro with Langchain: A Quick Guide to Mastering Langchain and Gemini Pro

Mohammed Ashraf
6 min readJan 13, 2024

--

Despite the initial dominance of ChatGPT and OpenAI’s GPT models, along with their collaboration with Microsoft, Google appeared to be relatively quiet in the AI space post the Transformer Model era. More than a year after the introduction of GPT models, Google made minimal strides, with the PaLM API failing to generate significant interest. However, the landscape changed abruptly with the unexpected unveiling of Gemini a series of foundational models by Google.

This guide delves into the recently launched Gemini API, conducting a practical test. It concludes with a step-by-step tutorial on how to use the Gemini chat model with Langchain.

you can download the NoteBook :

To use Google AI’s Gemini and Gemini-Vision models, you need to install the ‘langchain-google-genai’ package. Run the following command

!pip install -U --quiet langchain-google-genai  langchain

Try a Simple Example

To get started, let’s try a simple example using the Gemini-Pro model.

from langchain_google_genai import ChatGoogleGenerativeAI

# setup the gemini pro
gemini_llm = ChatGoogleGenerativeAI(model="gemini-pro",temperature=0.3)

result = gemini_llm.invoke("what is google vertex ai?")
print(result.content)

This is pretty simple same as we used to do this other LLM models.

Try With Prompt Template

please note that the Gemini doesn’t support SystemMessage at the moment, but it can be added to the first human message in the row.

from langchain_core.messages import HumanMessage, SystemMessage
from langchain.prompts import ChatPromptTemplate
from langchain.prompts import HumanMessagePromptTemplate

chat_template = ChatPromptTemplate.from_messages(
[
SystemMessage(
content=(
"return the sentiment for the given text in json fromat the sentiment value can be 'nagative','positive'"
)
),
HumanMessagePromptTemplate.from_template("{text}"),
]
)

chat_message = chat_template.format_messages(text="i don' like weather today.")
llm = ChatGoogleGenerativeAI(model="gemini-pro",
temperature=0.3, convert_system_message_to_human=True) # set the convert_system_message_to_human to true

the ‘convert_system_message_to_human’ will convert the system message in the prompt template into a human message. not to run

llm.invoke(chat_message)
AIMessage(content='```json\n{\n  "sentiment": "negative"\n}\n```')

Use the Output parser to get formatted output

As observed, the desired output from the model is in JSON format. While the model provides us with the JSON, it is in string format. To rectify this, we can employ a Langchain output parser to convert the output into the correct JSON format.

here is an example.

# import the json oupput parser from the langchain core
from langchain_core.output_parsers import JsonOutputParser

# define the parser object
parser = JsonOutputParser()

# create a chain
chain = llm | parser

sentiment = chain.invoke(chat_message)

print(sentiment)
{'sentiment': 'negative'}

We can leverage the Pydantic parser or establish a tagging chain to present the information in a more informative manner.

Tagging Chain with Gemini Pro

from typing import List

from langchain.output_parsers import PydanticOutputParser
from langchain.prompts import PromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field, validator

define the tag pedantic object

class Tags(BaseModel):
sentiment: str = Field(..., enum=["happy", "neutral", "sad"])
aggressiveness: int = Field(
...,
description="describes how aggressive the statement is, the higher the number the more aggressive",
enum=[1, 2, 3, 4, 5],
)
language: str = Field(
..., enum=["spanish", "english", "french", "german", "italian"]
)

once defined create a chain

# Set up a parser + inject instructions into the prompt template.
parser = PydanticOutputParser(pydantic_object=Tags)

prompt = PromptTemplate(
template="Answer the user query.\n{format_instructions}\n{query}\n",
input_variables=["query"],
partial_variables={"format_instructions": parser.get_format_instructions()},
)


chain = prompt | llm | parser

chain.invoke({"query": "i don' like weather today."})
Tags(sentiment='sad', aggressiveness=2, language='english')

Until now, we have explored and tested Gemini for standalone queries. Now, let’s take it a step further and venture into the realm of creating a conversational bot. In this section, we will delve into the intricacies of utilizing Gemini’s capabilities to construct a dynamic and engaging chatbot.

Creating a Conversational bot with Gemini Pro and Langchain

We employ the ConversationChain from LCEL to craft a straightforward bot capable of answering questions based on the previous context. This innovative approach enables our bot to maintain context throughout the conversation, enhancing its ability to provide relevant and context-aware responses.

from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationBufferMemory()
)
conversation.predict(input="Hi there!")
Hello! How may I assist you today?
conversation.predict(input="I'm doing well! Just having a conversation with an AI.")
That's great to hear! I'm glad you're having a good time. I'm always happy to chat with people and learn new things. What do you like to talk about?

Adding a custom prompt to the conversation chain

PROMPT_TEMPLATE = """The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
{history}
Human: {input}
AI Assistant:
"""

PROMPT = PromptTemplate(
input_variables=["history", "input"], template=PROMPT_TEMPLATE
)
conversation = ConversationChain(
llm=llm,
verbose=True,
prompt=PROMPT,
memory=ConversationBufferMemory(ai_prefix="AI Assistant")
)

Let’s Test:

conversation.predict(input="who is Gojo?")
Gojo Satoru is a character in the popular manga and anime series Jujutsu Kaisen. He is a powerful jujutsu sorcerer and a teacher at Tokyo Jujutsu High. Gojo is known for his eccentric personality, his immense strength, and his unique appearance, which includes white hair and blue eyes. He is also a skilled swordsman and hand-to-hand combatant.

Gojo is a complex and enigmatic character. He is often portrayed as aloof and carefree, but he also has a deep sense of justice and compassion. He is fiercely loyal to his students and friends, and he is always willing to fight for what he believes in.

Gojo is one of the most popular characters in Jujutsu Kaisen, and he has been praised for his unique design, his powerful abilities, and his complex personality. He is a fan favorite, and he is sure to continue to be a popular character for years to come.
conversation.predict(input="What is his power?")
Gojo Satoru's power is known as "Limitless," and it is one of the strongest techniques in the Jujutsu Kaisen universe. Limitless allows Gojo to manipulate space and time around him, creating a variety of powerful effects.

One of the most notable aspects of Limitless is its ability to create an "Infinity" barrier around Gojo. This barrier is impenetrable, and it prevents any attacks from reaching him. Gojo can also use Limitless to teleport himself and others, and he can even use it to stop time for a brief period.

In addition to its defensive capabilities, Limitless also grants Gojo a number of offensive abilities. He can use it to fire powerful blasts of energy, and he can also create powerful shockwaves by clapping his hands. Gojo is also a skilled swordsman, and he can use Limitless to enhance his swordsmanship.

Overall, Limitless is an incredibly versatile and powerful technique that makes Gojo one of the strongest characters in Jujutsu Kaisen.

As demonstrated, we have successfully built a conversational bot that provides answers based on the context of the ongoing conversation. This achievement showcases the effectiveness of our approach in creating a dynamic and context-aware bot. Now, you can experience firsthand how our conversational bot seamlessly generates responses tailored to the specific queries within the conversation, offering a more interactive and personalized user experience.

Conclusion

In conclusion, the Gemini Pro emerges as a powerful language model, demonstrating its capabilities to rival OpenAI’s Chat API in various aspects. However, it’s important to note a notable limitation in terms of token consumption, with Gemini Pro having an 8,000 token limit, whereas OpenAI’s GPT models boast higher limits — 16,000 for GPT-3 and 32,000 for GPT-4. Despite this constraint, the nuanced strengths and potential applications of Gemini Pro make it a compelling choice, highlighting the importance of evaluating specific project requirements when selecting the most suitable language model for a given task.

Happy exploring and coding!

--

--

Mohammed Ashraf

Dedicated GenAI Enthusiast crafting the future of AI with fervor and precision. Seamlessly blending cutting-edge algorithms with intuitive user experiences. 🌟