search
HomeBackend DevelopmentPython TutorialUtilizing Generative AI with Python

AI is the future, and as a software engineer, it’s the hottest field to get into. Leveraging LLMs in your code enables you to build smarter applications that handle complex tasks like real-time sentiment analysis or interpreting user-generated content. Integrating LLMs makes your software more responsive and capable, enhancing user experiences and automation.

This post is an introduction on how to make LLM calls using Python so you can start adding these powerful capabilities to your own code.

We’ll start off by making a chatbot for any character of your choosing. Then, you'll learn how to summarize smaller texts, and even move up to summarizing whole books. Lastly, you'll learn how to re-prompt and analyze results provided by the LLM.

Making our first LLM Request

For the LLM requests, we will be using Groq. If you create an account there, you can use their API and make LLM requests for free.

In order to use Python for these requests, install the Groq python package by running pip install groq. Then, we'll import it in our code like so:

import os
from groq import Groq

client = Groq(
    api_key=os.environ.get("GROQ_API_KEY"),
)

Be sure to set the api key as an environment variable.

A simple LLM request can be made by adding:

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Explain formula 1.",
        }
    ],
    model="llama3-8b-8192",
)
print(chat_completion.choices[0].message.content)

In this case, we ask the LLM to explain what formula 1 is. The output from llama3-8b should be printed once you run the program in your console. You can play around with this and switch the model, as well as the prompt.

Creating a custom Chatbot

Now, let's create a chatbot for any character you like—Mario, for example. Right now, the LLM responds in a neutral/informative tone. However, by giving the LLM a system role, we can make sure it responds just like Mario would, adding personality and fun to the conversation. This sets the tone for interactions, so you’ll get playful and iconic responses like “It’s-a me, Mario!” to keep things engaging.

Let's add a system role to our request:

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "system",
            "content": "You are a super mario chatbot. Always answer in his style, and create witty responses."
        },
        {
            "role": "user",
            "content": "Explain formula 1.",
        }
    ],
    model="llama3-8b-8192",
)
print(chat_completion.choices[0].message.content)

Now, the LLM will explain what Formula 1 is in terms of Mario Kart!

Utilizing Generative AI with Python

System roles are great for other use cases too, like virtual customer support agents, educational tutors, or creative writing helpers, making sure the LLM responds in a way that fits each role’s specific vibe and needs.

Summarizing Text

Now that we know a bit about how to make LLM requests with a specific prompt & system role, let's try and create a summarization tool.

Create a text file in the same directory called article.txt, and paste in any article of your choice. For this step, make sure the article is not too long.

In the code, let's first load in that text.

import os
from groq import Groq

client = Groq(
    api_key=os.environ.get("GROQ_API_KEY"),
)

Now, let's create a prompt that we can send to the LLM, telling it to summarize the text in bullet points.

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Explain formula 1.",
        }
    ],
    model="llama3-8b-8192",
)
print(chat_completion.choices[0].message.content)

We first write out the prompt, giving the LLM clear and concise instructions. Then, we provide the text that it should summarize.

Now, all we have to do is call the LLM with that prompt we just created:

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "system",
            "content": "You are a super mario chatbot. Always answer in his style, and create witty responses."
        },
        {
            "role": "user",
            "content": "Explain formula 1.",
        }
    ],
    model="llama3-8b-8192",
)
print(chat_completion.choices[0].message.content)

Run this, and you should see a bullet point summary of the article you gave the LLM!

Now, try and paste in a really long article, or maybe even a whole book -- like the Metamorphosis by Franz Kafka.

Notice that the LLM comes back with an error. You gave it too much to summarize all at once.

Summarizing a Book

Utilizing Generative AI with Python

The context window in an LLM refers to the amount of text it can process and remember in a single call. This means while it’s great for summarizing an article in one go, it can't handle a whole book at once because the text exceeds its capacity to take in and generate a coherent response.

So, how do we fix this? We can do so by 'chunking' the book. We split the book into 'chunks' that are manageable for the LLM, and tell it to summarize those. Then, once we have summaries for each of the chunks, we can summarize those summaries into one coherent summary.

You can split the string into chunks like so (be sure to import textwrap):

with open('article.txt', 'r') as file:
    content = file.read()

You can change around the width later, and see what you prefer and gives you the best results.

Now that we have all these chunks, let's summarize each of them and save the response inside a variable called answers.

prompt = f"""
Summarize the following text in bullet points for easy reading.

Text:
{content}
"""

If you run this code and print answers, you should see a long string with bullet point summaries for each 'chunk'/section it created.

Now, all we have to do is use the LLM one more time in order to create one coherent summary using all the section summaries.

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": prompt,
        }
    ],
    model="llama3-8b-8192",
)
print(chat_completion.choices[0].message.content)

Now, when you run the code, you should see one summary of the whole book! Remarkable, right?

Note: Depending on how big the book is, you might have to 'chunk' multiple times/tell the LLM to provide shorter responses. If there are too many 'chunk' summaries, the final summarization prompt might still be too large.

Re-prompting

You might have noticed that, even though we told the LLM to respond with bullet points, for example, it does not always provide the same response. Sometimes, it might add a header or a little explanation. Sometimes, it might just provide the bullet points.

As a programmer, this might make it difficult to sometimes process the results. How do we make sure the LLM provides more consistent answers in a specific format?

Let's make a sentiment analysis tool. We will feed the LLM a sad story, and tell it to come up with a sentiment score from -1 to 1.

Like so:

import os
from groq import Groq

client = Groq(
    api_key=os.environ.get("GROQ_API_KEY"),
)

If you run this multiple times, you can see that the response is not always the format that we specified. However, if we wanted to rely on that format to extract the number and perform further calculations, that is frustrating. Improper handling might cause our program to crash.

Re-prompting is the process of adjusting and refining the input given to an LLM to guide it toward a desired response or format. For validating a format for a sentiment tool that requires the output as "Sentiment: 0.5", you can re-prompt the LLM by tweaking your prompt to clearly instruct the model to return only the sentiment score in that exact format, ensuring consistency in the response.

We can create a function that checks whether the expected format was provided using Regex (so be sure to import regex).

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Explain formula 1.",
        }
    ],
    model="llama3-8b-8192",
)
print(chat_completion.choices[0].message.content)

Now, after we get the response from the LLM, we can call that function. If the function returns true, then we know we have the correct format. If it returns false, then we know that we should re-prompt the LLM and try again.

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "system",
            "content": "You are a super mario chatbot. Always answer in his style, and create witty responses."
        },
        {
            "role": "user",
            "content": "Explain formula 1.",
        }
    ],
    model="llama3-8b-8192",
)
print(chat_completion.choices[0].message.content)

Of course, this is very basic re-prompting. The LLM could still provide the incorrect format in this second LLM call. However, you should have a much higher success rate now of consistently formatted response.

With these tools and techniques, you’re now equipped to integrate LLMs into your Python code and validate outputs effectively. Please feel free to comment with any questions!

If you would like to see the full code, please visit the Github repository.

P.S: This is the blog post version of a workshop I gave to SCU’s ACM chapter.

The above is the detailed content of Utilizing Generative AI with Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Merging Lists in Python: Choosing the Right MethodMerging Lists in Python: Choosing the Right MethodMay 14, 2025 am 12:11 AM

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

How to concatenate two lists in python 3?How to concatenate two lists in python 3?May 14, 2025 am 12:09 AM

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Python concatenate list stringsPython concatenate list stringsMay 14, 2025 am 12:08 AM

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

Python execution, what is that?Python execution, what is that?May 14, 2025 am 12:06 AM

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Python: what are the key featuresPython: what are the key featuresMay 14, 2025 am 12:02 AM

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools