Tuesday, 22 April 2025

Getting Started with Ollama Chat API: Building an AI-Powered Chat Application

In this post, I am going to explain Ollama's Chat API and how it can be used to interact with AI-powered models like "llama3.2".

 

What is Ollama Chat API?

Ollama Chat API is a platform that provides seamless access to conversational AI models. It simplifies integrating and using language models in your applications, making it perfect for developers looking to build chatbots, Q&A systems, and more.

 

helloChat.py 

import ollama

response = ollama.chat(
    model = "llama3.2",
    messages = [
        {"role" : "user", "content" : "Tell me about Solar System"}
    ]
)

print(response["message"]["content"])

Output

The Solar System is a vast and fascinating place, consisting of eight planets, dwarf planets, asteroids, comets, and other celestial objects that orbit around the Sun. Here's an overview:

**Structure of the Solar System:**

1. **Sun:** The center of our solar system, the Sun is a massive ball of hot, glowing gas.
2. **Inner Planets:** Mercury, Venus, Earth, and Mars are the four inner planets closest to the Sun.
3. **Outer Planets:** Jupiter, Saturn, Uranus, and Neptune are the four outer planets farthest from the Sun.
4. **Dwarf Planets:** Pluto, Eris, Ceres, Haumea, and Makemake are the five officially recognized dwarf planets in our solar system.

**The Eight Planets:**

1. **Mercury:** The smallest planet, closest to the Sun, with extremely high temperatures during the day and freezing temperatures at night.
2. **Venus:** The hottest planet, with a thick atmosphere that traps heat, making it inhospitable to life.
3. **Earth:** Our home planet, with a diverse range of environments and life forms.
4. **Mars:** A rocky planet with a thin atmosphere, considered a potential candidate for human habitation.
5. **Jupiter:** The largest planet, a gas giant with massive storms like the Great Red Spot.
6. **Saturn:** Another gas giant, known for its stunning ring system and numerous moons.
7. **Uranus:** An icy planet with a tilted axis that causes extreme seasons.
8. **Neptune:** The farthest planet from the Sun, with strong winds and massive storm systems.

**Other Celestial Objects:**

1. **Asteroids:** Small rocky objects in orbit around the Sun, found mostly in the asteroid belt between Mars and Jupiter.
2. **Comets:** Icy bodies that release gas and dust as they approach the Sun, creating spectacular tails of debris.
3. **Kuiper Belt:** A region beyond Neptune's orbit filled with small, icy bodies and other celestial objects.
4. **Oort Cloud:** A distant, spherical shell of icy bodies surrounding our solar system.

**Key Features:**

1. **Orbital Periods:** The time it takes for an object to complete one orbit around the Sun varies greatly between planets (e.g., Mercury's 88 Earth days vs. Neptune's 165 Earth years).
2. **Atmospheres:** Many planets have atmospheres, which can be thick or thin, and affect their surface temperatures.
3. **Moons:** Most planets have natural satellites (moons), some of which are large enough to be considered dwarf planets.

**Exploration and Research:**

The Solar System has been extensively explored through spacecraft, telescopes, and other scientific instruments. Ongoing and future missions aim to:

1. Explore Mars and the search for life beyond Earth.
2. Study the outer planets and their moons in greater detail.
3. Investigate asteroids and comets to better understand their origins.

This is a brief introduction to our amazing Solar System. There's still much to learn, and scientists continue to explore and unravel its secrets!

 

response = ollama.chat(

    model="llama3.2",

    messages=[

        {"role": "user", "content": "Tell me about the Solar System"}

    ]

)

 

This line calls the ollama.chat function, which sends a query to the Ollama API. The API processes the query using the specified model and returns the AI's response in the response variable. The response is typically a JSON object containing details of the conversation.

 

model="llama3.2"

Specifies the AI model to use for processing the query. In this case, it's "llama3.2".

 

messages=[...]

Defines the sequence of conversational messages. It takes a list of dictionaries, where each dictionary represents a single message in the conversation.

 

·      "role": Specifies who is speaking. The roles typically include:

o   "user": The person (or system) asking the question.

o   "assistant": The AI responding to the user.

 

·      "content": The actual text of the message. Here, the user asks, "Tell me about the Solar System".

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment