Ollama
¶
Ollama is an open-source project that serves as a powerful and user-friendly platform for running LLMs on your local machine. So, it lets you download and run large language models (LLMs) on your own machine, without relying on cloud-hosted services. Ollama server is running in the docker and needs to import the python library to work with ollama.
In [1]:
import ollama
Choose your model. The current downloaded model in the docker is gemma3:1b, which consists of 1B parameters and has size of 815MB
In [2]:
model_name="gemma3:1b"
In [3]:
def ask_ollama(question):
response = ollama.chat(model=model_name, messages=[{'role': 'user', 'content': question}])
return response['message']['content']
In [4]:
print(ask_ollama('how you can use continuous chat in ollama python, so the response will depend on all the previous questions'))
Okay, let's dive into how to leverage continuous chat in Ollama (or any conversational AI framework) to maintain context across multiple turns and questions. This is a key element of building a truly conversational experience. Here's a breakdown of the techniques and considerations, broken down into manageable steps and code examples:
**1. Understanding the Core Concept: Context Management**
The essence of continuous chat is maintaining a "state" – a record of the conversation's history – so the AI remembers what's been said previously. Ollama itself doesn't natively have robust context management *within* a single chat session. We'll need to build a layer on top of it.
**2. Techniques & Implementation**
Here's a combination of techniques you can use:
* **Conversation History (Basic)**: Simply store the last few turns of the conversation in a dictionary (or a list, depending on your needs). Ollama already does this in a manner that's suitable for basic chat.
* **Memory Modules (For Enhanced Context)**: The best approach involves using memory modules, which are designed specifically for this purpose. These modules will keep track of the conversation state.
* **Vector Database (Advanced - More Complex)**: For very large contexts (like long conversations), a vector database becomes necessary. It represents conversations as embeddings (numerical representations) and allows you to find the most relevant past turns based on similarity. This is much more resource-intensive but provides more powerful context retrieval.
Let’s explore the first two:
**3. Using Conversation History (Basic)**
This is the simplest starting point. We'll create a dictionary to store the history:
```python
def get_conversation_history(prompt, max_history_length=5):
"""Retrieves the last N turns of the conversation."""
history = []
for i in range(max_history_length):
current_turn = f"Previous turn: {prompt}" # Example prompt
history.append(current_turn)
if i < max_history_length - 1:
history.append(f"Next turn: {prompt}")
return history
# Example usage:
prompt = "Tell me about apples."
history = get_conversation_history(prompt)
print(history)
```
**4. Using a Memory Module (Simplified - a list of previous turns)**
Ollama and other frameworks use a list of turns to represent the conversation. This is where we'll implement our memory:
```python
def add_turn(prompt, history):
"""Adds a turn to the conversation history."""
history.append(prompt)
return history
def get_conversation_history(prompt, max_history_length=5):
"""Retrieves the last N turns of the conversation."""
history = []
for i in range(max_history_length):
current_turn = f"Previous turn: {prompt}"
history.append(current_turn)
if i < max_history_length - 1:
history.append(f"Next turn: {prompt}")
return history
# Example usage:
prompt = "What is a blueberry?"
history = get_conversation_history(prompt)
print(history)
```
**5. Key Strategies for Maintaining Context (Improving the Above)**
* **Prompt Engineering:** The *way* you phrase your prompts is crucial. Include context within the prompt itself. For example:
```python
def process_user_input(user_input):
prompt = "You mentioned earlier that you like chocolate. What other types of chocolate do you enjoy?"
# ... rest of your logic here
```
* **Entity Linking (More Advanced):** Store entities (named things) extracted from the user's input. Ollama uses entity linking to understand what the user is talking about. You'd need to integrate this into the memory layer.
* **Semantic Memory:** Store the meaning of words, not just the text. This is a more advanced technique for long-term context.
**6. Integrating into Ollama (or your framework)**
* **Custom Chat Loop:** You'll need to modify your Ollama chat loop to include the `get_conversation_history` function.
* **Context Updates:** When a new user input comes in, you need to *update* the `history` and pass it to the AI model. This could involve:
* **Summarizing the Conversation:** Quickly create a short summary of the recent conversation.
* **Adding a Snippet:** Include a small portion of the recent conversation (the last few turns).
**7. Example - Longer Context with a List of Previous Turns**
```python
def add_turn(prompt, history):
"""Adds a turn to the conversation history."""
history.append(prompt)
return history
def get_conversation_history(prompt, max_history_length=5):
"""Retrieves the last N turns of the conversation."""
history = []
for i in range(max_history_length):
current_turn = f"Previous turn: {prompt}"
history.append(current_turn)
if i < max_history_length - 1:
history.append(f"Next turn: {prompt}")
return history
# Example usage:
prompt = "Tell me about cats and dogs."
history = get_conversation_history(prompt)
print(history)
```
**Important Considerations**
* **Scalability:** For very long conversations, vector databases become essential.
* **Cost:** Vector databases have costs associated with them.
* **Model Capabilities:** The AI model you're using will influence how effectively it can handle context.
**To help me refine this further and give you even more tailored advice, could you tell me:**
* What kind of interaction are you aiming for? (e.g., casual conversation, question answering, creative writing, etc.)
* What is your existing code setup? (Are you using a specific framework or library?)
* What are the key aspects of context you're trying to preserve? (e.g., the most recent turns, the overall topic of the conversation, etc.)
Continuous chat in Ollama¶
To maintain a conversational history where the model's response depends on all previous questions and answers, you need to store coversation history in a list of messages
In [5]:
history = []
def ask_ollama(question):
#add new message to history
history.append({'role': 'user', 'content': question})
#send full history to ollama
response = ollama.chat(model=model_name, messages=history)
#add assistant response to history
history.append({'role': 'assistant', 'content': response['message']['content']})
return response['message']['content']
In [6]:
print(ask_ollama("where is United Arab Emirates"))
The United Arab Emirates (UAE) is a fascinating and geographically unique country located in the Middle East. Here's a breakdown of its location: **Officially:** * **Location:** Located on the southeastern coast of the Arabian Peninsula, in the Persian Gulf. * **Coordinates:** Approximately 24° North, 55° East. **Specifically:** * **Gulf Region:** It sits within the Arabian Peninsula, bordering Saudi Arabia to the west, Qatar to the north, and Kuwait to the east. * **Sea:** It's situated on the Persian Gulf, which is a large, shallow body of water. * **Coastal:** It’s primarily an island nation with numerous smaller islands and peninsulas. **Key Geographic Features:** * **Persian Gulf:** It's a crucial part of the Persian Gulf, making it a vital shipping route. * **Desert Landscapes:** A significant portion of the UAE is desert, including vast sand dunes, rocky hills, and oases. * **Coastal Beaches:** The coastline is incredibly diverse, with stunning beaches, coral reefs, and lagoons. **In short, the UAE is a country that is geographically positioned in the heart of the Middle East, known for its desert landscapes, rich history, and vibrant culture.** Do you want to know more about a specific aspect of the UAE, such as its history, culture, or attractions?
In [7]:
print(ask_ollama("its culture?"))
Okay, let’s dive into the rich and diverse culture of the UAE! It’s a fascinating blend of Arab traditions, influences from Persian, Indian, and Chinese cultures, and a unique blend of modernity and tradition. Here’s a breakdown of key elements: **1. Religion & Spirituality:** * **Islam:** The overwhelming majority of the UAE population is Muslim, and Islam is the state religion. You’ll see mosques everywhere – grand, ornate ones, and smaller, more traditional ones. * **Respect for the Prophet:** The UAE deeply reveres the Prophet Muhammad and Islamic teachings. **2. Traditions & Customs:** * **Ramadan:** A very important religious observance where Muslims refrain from eating, drinking, and smoking during the day. It’s a time of great community and reflection. * **Hajj & Umrah:** While less prevalent than in some other Muslim-majority countries, the UAE has a growing number of pilgrims travelling to Hajj (the pilgrimage to Mecca) and Umrah (the pilgrimage to Mecca). * **Family & Community:** Family ties are incredibly strong. Family gatherings, celebrations, and mutual support are highly valued. * **Hospitality:** The UAE is renowned for its warm hospitality. Guests are treated with immense respect and generosity. “Welcome to the UAE” is a common phrase. * **Dress Code:** While Western clothing is accepted in many cities, conservative attire is expected, especially during Ramadan and public holidays. Men typically wear a white shirt and pants, while women wear a headscarf (hijab) and a long skirt or dress. **3. Arts & Crafts:** * **Leatherwork:** A hugely important craft, especially in the emirates (city-states), with intricate designs and vibrant colors. * **Calligraphy:** Beautifully rendered Arabic script is a core element of the artistic tradition. * **Ceramics:** The UAE has a long history of ceramic production, producing colorful and detailed designs. * **Music & Dance:** Traditional music and dance forms are still practiced, often incorporating elements of Arabic, Persian, and Indian music. Traditional dances like *Al-Quraish* are particularly celebrated. * **Wood Carving:** Known for producing exquisite wood carvings, often depicting birds and geometric patterns. **4. Food Culture:** * **Arabic Cuisine:** The cuisine is heavily influenced by Arabic flavors, drawing from Persian, Indian, and Moroccan traditions. * **Meat & Seafood:** Meat dishes are very popular, particularly lamb and chicken. Seafood is abundant, especially along the coast. * **Curries:** A huge variety of curries are eaten, often flavored with spices like cinnamon, cardamom, and cloves. * **Dates & Sweets:** Dates are a staple, and the UAE is famous for its delicious sweets and pastries. * **Traditional Dishes:** Examples include *Machboos* (a meat stew), *Kabsa* (a rice dish), and *Harees* (a lentil and meat stew). **5. Modern Culture & Trends:** * **Modern Architecture:** The UAE is undergoing a massive architectural transformation, with gleaming skyscrapers and modern designs. * **Technology:** The UAE is a global leader in technology, with a strong focus on innovation and development. * **Diversity & Multiculturalism:** The UAE is a melting pot of cultures, resulting in a remarkably diverse and tolerant society. * **Emphasis on Education:** Education is highly valued, and there's a significant emphasis on scientific and technological education. **6. Key Emirates (Illustrative Examples):** * **Dubai:** Known for its luxury, modern architecture, and bustling atmosphere. * **Abu Dhabi:** The capital, a center for culture, arts, and innovation, boasting the Sheikh Zayed Grand Mosque. * **Sharjah:** A historical city with a blend of Emirati and Arab influences. **Resources for Further Learning:** * **The UAE Government Website:** [https://www.gov.ae/](https://www.gov.ae/) - Official source for information. * **Culture Trip:** [https://www.culturetrip.com/guide/united-arab-emirates/culture/](https://www.culturetrip.com/guide/united-arab-emirates/culture/) - Offers a great overview. Do you want me to delve deeper into a specific area of UAE culture, like their celebrations, history, or a particular emirate’s traditions?
In [ ]: