Overview
Contextual AI’s Grounded Language Model (GLM) is the most grounded language model in the world, making it the best language model to use for RAG and agentic use cases for which minimizing hallucinations is critical.Global Variables & Examples
First, we will set up the global variables and examples we’ll use with each different implementation method.Copy
from google.colab import userdata
api_key = userdata.get("API_TOKEN")
base_url = "https://api.contextual.ai/v1"
generate_api_endpoint = f"{base_url}/generate"
Copy
# Example conversation messages
messages = [
{
"role": "user",
"content": "What are the most promising renewable energy technologies for addressing climate change in developing nations?"
},
{
"role": "assistant",
"content": "Based on current research, solar and wind power show significant potential for developing nations due to decreasing costs and scalability. Would you like to know more about specific implementation challenges and success stories?"
},
{
"role": "user",
"content": "Yes, please tell me about successful solar implementations in Africa and their economic impact, particularly focusing on rural electrification."
}
]
# Detailed knowledge sources with varied information
knowledge = [
"""According to the International Renewable Energy Agency (IRENA) 2023 report:
- Solar PV installations in Africa reached 10.4 GW in 2022
- The cost of solar PV modules decreased by 80% between 2010 and 2022
- Rural electrification projects have provided power to 17 million households""",
"""Case Study: Rural Electrification in Kenya (2020-2023)
- 2.5 million households connected through mini-grid systems
- Average household income increased by 35% after electrification
- Local businesses reported 47% growth in revenue
- Education outcomes improved with 3 additional study hours per day""",
"""Economic Analysis of Solar Projects in Sub-Saharan Africa:
- Job creation: 25 jobs per MW of installed capacity
- ROI average of 12-15% for mini-grid projects
- Reduced energy costs by 60% compared to diesel generators
- Carbon emissions reduction: 2.3 million tonnes CO2 equivalent""",
"""Technical Specifications and Best Practices:
- Optimal solar panel efficiency in African climate conditions: 15-22%
- Battery storage requirements: 4-8 kWh per household
- Maintenance costs: $0.02-0.04 per kWh
- Expected system lifetime: 20-25 years""",
"""Social Impact Assessment:
- Women-led businesses increased by 45% in electrified areas
- Healthcare facilities reported 72% improvement in service delivery
- Mobile money usage increased by 60%
- Agricultural productivity improved by 28% with electric irrigation"""
]
Direct API implementation
Copy
import requests
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {api_key}"
}
Copy
payload = {
"model": "v1",
"messages": messages,
"knowledge": knowledge,
"avoid_commentary": False,
"max_new_tokens": 1024,
"temperature": 0,
"top_p": 0.9
}
generate_response = requests.post(generate_api_endpoint, json=payload, headers=headers)
print(generate_response.json()['response'])
Copy
Let me share the current state of solar energy in Africa, focusing on rural electrification and its economic impact:
Africa has seen significant solar PV adoption, with total installations reaching 10.4 GW as of 2022, supported by an 80% decrease in solar PV module costs between 2010 and 2022.
The impact on rural communities has been particularly noteworthy:
Rural electrification initiatives have already connected 17 million households to power. A specific case study in Kenya (2020-2023) demonstrated impressive results:
- 2.5 million households were connected through mini-grid systems
- Average household income increased by 35%
- Local businesses saw 47% revenue growth
- Students gained 3 additional study hours per day
The economic benefits extend beyond household income:
The solar industry has created substantial economic opportunities:
- Each MW of installed capacity generates 25 jobs
- Mini-grid projects typically achieve 12-15% ROI
- Energy costs have been reduced by 60% compared to diesel generators
- The projects have resulted in 2.3 million tonnes CO2 equivalent in emissions reduction
The social impact has been particularly significant:
The data shows:
- 45% increase in women-led businesses
- 72% improvement in healthcare service delivery
- 60% increase in mobile money usage
- 28% improvement in agricultural productivity with electric irrigation
Python SDK
Copy
try:
from contextual import ContextualAI
except:
%pip install contextual-client
from contextual import ContextualAI
client = ContextualAI (api_key = api_key, base_url = base_url)
Copy
generate_response = client.generate.create(
model="v1",
messages=messages,
knowledge=knowledge,
avoid_commentary=False,
max_new_tokens=1024,
temperature=0,
top_p=0.9
)
print(generate_response.response)
Copy
Let me share the current state of solar energy in Africa, focusing on rural electrification and its economic impact:
Africa has seen significant solar PV adoption, with total installations reaching 10.4 GW as of 2022, supported by an 80% decrease in solar PV module costs between 2010 and 2022.
The impact on rural communities has been particularly noteworthy:
Rural electrification initiatives have already connected 17 million households to power. A specific case study in Kenya (2020-2023) demonstrated impressive results:
- 2.5 million households were connected through mini-grid systems
- Average household income increased by 35%
- Local businesses saw 47% revenue growth
- Students gained 3 additional study hours per day
The economic benefits extend beyond household income:
The solar industry has created substantial economic opportunities:
- Each MW of installed capacity generates 25 jobs
- Mini-grid projects typically achieve 12-15% ROI
- Energy costs have been reduced by 60% compared to diesel generators
- The projects have resulted in 2.3 million tonnes CO2 equivalent in emissions reduction
The social impact has been particularly significant:
The data shows:
- 45% increase in women-led businesses
- 72% improvement in healthcare service delivery
- 60% increase in mobile money usage
- 28% improvement in agricultural productivity with electric irrigation
Langchain
Copy
try:
from langchain_contextual import ChatContextual
except:
%pip install langchain-contextual
from langchain_contextual import ChatContextual
# intialize Contextual llm via langchain_contextual
llm = ChatContextual(
model="v1",
api_key=api_key,
)
ai_msg = llm.invoke(
messages,
knowledge=knowledge,
avoid_commentary=False,
max_new_tokens=1024,
temperature=0,
top_p=0.9
)
print(ai_msg.content)
Copy
Let me share the current state of solar energy in Africa, focusing on rural electrification and its economic impact:
Africa has seen significant solar PV adoption, with total installations reaching 10.4 GW as of 2022, supported by an 80% decrease in solar PV module costs between 2010 and 2022.
The impact on rural communities has been particularly noteworthy:
Rural electrification initiatives have already connected 17 million households to power. A specific case study in Kenya (2020-2023) demonstrated impressive results:
- 2.5 million households were connected through mini-grid systems
- Average household income increased by 35%
- Local businesses saw 47% revenue growth
- Students gained 3 additional study hours per day
The economic benefits extend beyond household income:
The solar industry has created substantial economic opportunities:
- Each MW of installed capacity generates 25 jobs
- Mini-grid projects typically achieve 12-15% ROI
- Energy costs have been reduced by 60% compared to diesel generators
- The projects have resulted in 2.3 million tonnes CO2 equivalent in emissions reduction
The social impact has been particularly significant:
The data shows:
- 45% increase in women-led businesses
- 72% improvement in healthcare service delivery
- 60% increase in mobile money usage
- 28% improvement in agricultural productivity with electric irrigation
LLamaIndex
Copy
try:
from llama_index.llms.contextual import Contextual
from llama_index.core.chat_engine.types import ChatMessage
except:
%pip install -U llama-index-llms-contextual
from llama_index.llms.contextual import Contextual
from llama_index.core.chat_engine.types import ChatMessage
llm = Contextual(model="v1", api_key=api_key)
response = llm.complete(
messages[0]['content'],
knowledge=knowledge,
avoid_commentary=False,
max_new_tokens=1024,
temperature=0,
top_p=0.9
)
print(response.text)
Copy
Based on recent data and implementation results, solar energy emerges as a particularly promising renewable energy technology for developing nations. Here are the key findings:
As of 2022, solar PV installations in Africa have reached 10.4 GW, with solar PV module costs decreasing by 80% between 2010 and 2022.
The technology has demonstrated impressive economic and social impacts:
- Creates significant employment opportunities with 25 jobs per MW of installed capacity
- Delivers strong returns with 12-15% ROI for mini-grid projects
- Reduces energy costs by 60% compared to diesel generators
- Achieves substantial carbon savings of 2.3 million tonnes CO2 equivalent
The implementation data shows remarkable social benefits:
- Rural electrification projects have connected 17 million households
- In Kenya specifically, 2.5 million households have been connected through mini-grid systems
- Average household income increased by 35% after electrification
- Local businesses reported 47% growth in revenue
- Education outcomes improved with 3 additional study hours per day
From a technical perspective:
- Solar panels achieve optimal efficiency of 15-22% in African climate conditions
- Systems require 4-8 kWh of battery storage per household
- Maintenance costs are relatively low at $0.02-0.04 per kWh
- Systems have an expected lifetime of 20-25 years
These metrics demonstrate solar energy's potential as a viable solution for developing nations.
messages_llama_index = [ChatMessage(role=message['role'], content=message['content']) for message in messages]
response = llm.chat(
messages_llama_index,
knowledge_base=knowledge,
avoid_commentary=False,
max_new_tokens=1024,
temperature=0,
top_p=0.9
)
print(response)
Copy
assistant: Let me share the current state of solar energy in Africa, focusing on rural electrification and its economic impact:
Africa has seen significant solar PV adoption, with total installations reaching 10.4 GW as of 2022, supported by an 80% decrease in solar PV module costs between 2010 and 2022.
The impact on rural communities has been particularly noteworthy:
Rural electrification initiatives have already connected 17 million households to power. A specific case study in Kenya (2020-2023) demonstrated impressive results:
- 2.5 million households were connected through mini-grid systems
- Average household income increased by 35%
- Local businesses saw 47% revenue growth
- Students gained 3 additional study hours per day
The economic benefits extend beyond household income:
The solar industry has created substantial economic opportunities:
- Each MW of installed capacity generates 25 jobs
- Mini-grid projects typically achieve 12-15% ROI
- Energy costs have been reduced by 60% compared to diesel generators
- The projects have resulted in 2.3 million tonnes CO2 equivalent in emissions reduction
The social impact has been particularly significant:
The data shows:
- 45% increase in women-led businesses
- 72% improvement in healthcare service delivery
- 60% increase in mobile money usage
- 28% improvement in agricultural productivity with electric irrigation
Multi-Turn Conversations
The GLM supports multi-turn conversations, but with an important consideration: the model is stateless and knowledge does not automatically persist between turns. This means:- To continue a conversation using the same knowledge: You must provide your knowledge array again with each new request.
- To continue a conversation with additional information: You must combine your existing knowledge array with the new information and provide the complete updated array.
Copy
#@title First Turn of Conversation
user_msg_1 = "Tell me about the Emperor Penguin"
messages_1 = [
{
"role": "user",
"content": user_msg_1
}
]
knowledge_1 = [
"Emperor penguins (Aptenodytes forsteri) are the tallest and heaviest of all living penguin species, standing up to 122 cm (48 inches) tall and weighing up to 45 kg (99 pounds).",
"Emperor penguins are the only penguin species that breeds during the Antarctic winter, enduring temperatures as low as -60°C (-76°F) and wind speeds up to 200 km/h (124 mph).",
"Male Emperor penguins incubate their single egg for about 65-75 days while balancing it on their feet and covering it with a special brood pouch, during which time they don't eat and can lose up to 45% of their body weight.",
"Emperor penguins can dive deeper than any other bird, reaching depths of up to 565 meters (1,850 feet) and staying underwater for up to 22 minutes when hunting for fish, squid and krill."
]
response = client.generate.create(
knowledge=knowledge_1,
messages=messages_1,
model="v1",
)
returned_response = response.to_dict()['response']
print(f"User: {user_msg_1}\n\n")
print(f"Assistant: {returned_response}")
Copy
User: Tell me about the Emperor Penguin
Assistant: Let me tell you about the Emperor Penguin's remarkable characteristics and behaviors:
Emperor penguins are the largest penguin species, reaching heights of up to 122 cm (48 inches) and weighing up to 45 kg (99 pounds).
Their breeding habits are particularly fascinating:
They are unique among penguins as they breed during the Antarctic winter, withstanding extreme conditions including:
- Temperatures as low as -60°C (-76°F)
- Wind speeds up to 200 km/h (124 mph)
During breeding, male Emperor penguins take on remarkable parental responsibilities:
- They incubate a single egg for 65-75 days
- They balance the egg on their feet and cover it with a special brood pouch
- During this time, they fast and can lose up to 45% of their body weight
Their hunting abilities are equally impressive:
Emperor penguins are exceptional divers, capable of:
- Reaching depths of up to 565 meters (1,850 feet)
- Staying underwater for up to 22 minutes
- Hunting for fish, squid, and krill
Copy
#@title Second Turn (without Extending Knowledge)
user_msg_2 = "Compare the emperor penguin with the adelie penguin"
messages_2 = [
{
"role": "user",
"content": user_msg_1
},
{
"role": "assistant",
"content": returned_response,
},
{
"role": "user",
"content": user_msg_2
}
]
knowledge_2 = [
"Adélie penguins (Pygoscelis adeliae) are named after Adélie Land, a territory in Antarctica discovered by French explorer Jules Dumont d'Urville, who named it after his wife.",
"Adélie penguins are one of the most southerly distributed birds and one of only two penguin species that are truly Antarctic (the other being the Emperor penguin), breeding exclusively on the Antarctic continent and nearby islands.",
"Adélie penguins build nests made of small stones which they sometimes steal from neighboring nests, and males present these stones as gifts to potential mates during courtship.",
"With a distinctive white ring around their eyes, Adélie penguins are excellent swimmers that can travel up to 185 km (115 miles) round trip to forage for food and can reach swimming speeds of up to 45 km/h (28 mph)."
]
print('This is turn 2')
print(f"User: {user_msg_2}\n\n")
response_2_bad = client.generate.create(
knowledge=knowledge_2,
messages=messages_2,
model='v1',
)
print(f"Assistant: {response_2_bad.to_dict()['response']}")
Copy
This is turn 2
User: Compare the emperor penguin with the adelie penguin
Assistant: I can provide some basic comparisons between these two penguin species based on the available information:
Both Emperor and Adélie penguins are truly Antarctic species, with Adélie penguins breeding exclusively on the Antarctic continent and nearby islands.
Adélie penguins have distinctive physical characteristics, including a white ring around their eyes.
Regarding their behavior, Adélie penguins are known to:
- Build nests using small stones
- Sometimes steal stones from neighboring nests
- Use stones as gifts during courtship
- Travel up to 185 km (115 miles) for food
- Reach swimming speeds of up to 45 km/h (28 mph)
I don't have specific information about Emperor penguins in the provided documentation to make a more detailed comparison. If you'd like to know more about Emperor penguins specifically, I'd be happy to share that information if you provide it.
Copy
#@title Second Turn (with Extending Knowledge)
extended_knowledge = knowledge_1 + knowledge_2
print(f"User: {user_msg_2}\n\n")
response_2_good = client.generate.create(
knowledge=extended_knowledge,
messages=messages_2,
model='v1',
)
print(f"Assistant: {response_2_good.to_dict()['response']}")
Copy
User: Compare the emperor penguin with the adelie penguin
Assistant: Let me compare these two fascinating penguin species based on the available information:
Emperor penguins are the largest penguin species, reaching heights of up to 122 cm (48 inches) and weighing up to 45 kg (99 pounds).
Both species are well adapted to extreme Antarctic conditions, with Emperor penguins being the only species that breeds during the Antarctic winter, enduring temperatures as low as -60°C (-76°F) and wind speeds up to 200 km/h (124 mph).
Adélie penguins are one of the most southerly distributed birds and, along with Emperor penguins, are truly Antarctic, breeding exclusively on the Antarctic continent and nearby islands.
When it comes to breeding behaviors:
- Emperor penguins have males incubating eggs for 65-75 days while fasting and losing up to 45% of their body weight
- Adélie penguins build nests using small stones, which they sometimes take from neighboring nests
In terms of swimming abilities:
- Emperor penguins can dive to 565 meters (1,850 feet) and stay underwater for up to 22 minutes
- Adélie penguins can travel up to 185 km (115 miles) round trip while foraging and reach swimming speeds of up to 45 km/h (28 mph)
Additional Resources
- Read the blog post to learn more about the GLM and the criticality of groundedness in enterprise AI
- Access the related notebook that demonstrates how to use the GLM with the Contextual API directly, our Python SDK, and our Langchain package
- API Reference
- Python SDK
- Langchain Package
- LlamaIndex Integration