Introduction
As ai is taking over the world, large language models are in high demand in technology. Large language models generate text the same way a human does. They can be used to develop natural language processing (NLP) applications ranging from chatbots and text summarizers to translation applications, virtual assistants, etc.
Google launched its next generation model called Palm 2. This model excels in advanced scientific and mathematical operations and is used in reasoning and language translation. This model is trained on over 100 spoken word languages and over 20 programming languages.
As it is trained in multiple programming languages, it can be used to translate one programming language to another. For example, if you want to translate Python code to R or JavaScript code to TypeScript, etc., you can easily use Palm 2 to do it for you. Apart from this, it can generate idioms and phrases and easily break down a complex task into simpler tasks, making it much better than previous big language models.
Learning objectives
- Introduction to the Google Palm API
- Learn how to access the Palm API by generating an API key
- Using Python, learn how to generate simple text using the text model
- Learn how to create a simple chatbot using Python
- Finally, we look at how to use Langchain with the Palm API.
This article was published as part of the Data Science Blogathon.
palm API
With the Palm API, you can access the capabilities of Google’s generative ai models and develop exciting ai-based applications. However, if you want to interact directly with the Palm 2 model from the browser, you can use the “MakerSuite” browser-based IDE. But you can access the Palm 2 model using the Palm API to integrate large language models into your applications and create ai-powered applications using your company’s data.
Three different prompt interfaces have been designed and you can get started with any of them using the Palm API. They are:
- Text messages: You can use the model named “text-bison-001 to generate simple text. Using text prompts, you can generate text, generate code, edit text, retrieve information, extract data, etc.
- Data messages: Allow you to create messages in tabular format.
- chat messages: Chat prompts are used to start conversations. You can use the model named “chat-bison-001” to use chat services.
Access the Palm API
Navigate to website https://developers.generativeai.google/ and join the maker suite. You will be added to the waitlist and given access probably within 24 hours.
Generate an API key:
- You need to obtain your own API key to use the API.
- You can connect your app to the Palm API and access its services using the API key.
- Once your account is registered, you can generate it.
- Next, go ahead and generate your API key, as shown in the screenshot below:
Save the API key as we will use it more.
Setting the environment
To use the API with Python, install it using the command:
pip install google-generativeai
Next, we configure it using the API key that we generated earlier.
import google.generativeai as palm
palm.configure(api_key=API_KEY)
To list the available models, we write the below code:
models = (model for model in palm.list_models())
for model in models:
print(model.name)
Production:
models/chat-bison-001
models/text-bison-001
models/embedding-gecko-001
Generate text
We use the “text-bison-001” model to generate text and pass GenerateTextRequest. The generate_text() function takes two parameters i.e. a model and a message. We pass the model as “text-bison-001” and the message contains the input string.
Explanation:
- In the following example, we pass the model_id variable with the model name and a request variable containing the input text.
- We then pass model_id as model and message as message to the generate_text() method.
- The temperature parameter indicates how random the response will be. In other words, if you want the model to be more creative, you can give it a value of 0.3.
- Finally, the “max_tokens” parameter indicates the maximum number of tokens that the model output can contain. One token can contain approximately 4 tokens. However, if you do not specify it, it will be assigned a default value of 64.
Example 1
model_id="models/text-bison-001"
prompt=""'write a cover letter for a data science job applicaton.
Summarize it to two paragraphs of 50 words each. '''
completion=palm.generate_text(
model=model_id,
prompt=prompt,
temperature=0.99,
max_output_tokens=800,
)
print(completion.result)
Production:
We define a while loop that requests information and generates a response. The response.last statement prints the response.
model_id="models/chat-bison-001"
prompt="I need help with a job interview for a data analyst job. Can you help me?"
examples=(
('Hello', 'Hi there mr. How can I be assistant?'),
('I want to get a High paying Job','I can work harder')
)
response=palm.chat(messages=prompt, temperature=0.2, context="Speak like a CEO", examples=examples)
for messages in response.messages:
print(messages('author'),messages('content'))
while True:
s=input()
response=response.reply(s)
print(response.last)
Production:
Using Palm API with LangChain
LangChain is an open source framework that allows you to connect large language models to your applications. To use the Palm API with langchain, we import GooglePalmEmbeddings from langchain.embeddings. Langchain has an embedding class that provides a standard interface for various text embedding models such as OpenAI, HuggingFace, etc.
We pass the prompts as an array, as shown in the following example. Then, we call the llm._generate() function and pass the messages array as a parameter.
from langchain.embeddings import GooglePalmEmbeddings
from langchain.llms import GooglePalm
llm=GooglePalm(google_api_key=API_KEY)
llm.temperature=0.2
prompts=("How to Calculate the area of a triangle?","How many sides are there for a polygon?")
llm_result= llm._generate(prompts)
res=llm_result.generations
print(res(0)(0).text)
print(res(1)(0).text)
Production:
Prompt 1
1.
**Find the base and height of the triangle.
** The base is the length of the side of the triangle that is parallel to the ground, and the height is the length of the line segment that is perpendicular to the base and intersects the opposite vertex.
2.
**Multiply the base and height and divide by 2.
** The formula for the area of a triangle is A = 1/2 * b * h.
For example, if a triangle has a base of 5 cm and a height of 4 cm, its area would be 1/2 * 5 * 4 = 10 cm2.
Prompt 2
3
Conclusion
In this article, we introduce Google’s latest Palm 2 model and how it is better than previous models. We then learned how to use the Palm API with the Python programming language. We then discussed how to develop simple applications and generate text and chats. Finally, we cover how to integrate it using the Langchain framework.
Key takeaways
- Palm API allows users to develop applications using large language models
- Palm API provides multiple text generation services, such as a text service to generate text and a chat service to generate chat conversations.
- Google generative-ai is Palm’s Python API library and can be easily installed using the pip command.
Frequent questions
A. To get started quickly with the Palm API in Python, you can install a library using the pip command – pip install google generative-ai.
A. Yes, you can access Google’s large language models and develop applications using the Palm API.
A. Yes, the Google Palm API and MakerSuite are available for public preview.
A. Google’s Palm 2 model was trained in more than 20 programming languages and can generate code in several programming languages.
A. Palm API comes with text and chat services. Provides multiple text generation capabilities.
The media shown in this article is not the property of Analytics Vidhya and is used at the author’s discretion.