How to Augment GPT-4 with Qdrant to Elevate Its Poetry Composition Capabilities

Akriti Upadhyay
10 min readDec 14, 2023

Introduction

In the realm of artificial intelligence, language models like GPT-4 have revolutionized the way we interact with machines. These models, trained on vast amounts of text data, can generate human-like text, making them useful in a variety of applications. One such application is poetry composition.

However, despite their impressive capabilities, these models can sometimes produce outputs that are nonsensical or lack depth. This is where Qdrant, a vector database for efficient storage and retrieval of high-dimensional data, comes into play. By augmenting GPT-4 with Qdrant, we can elevate its poetry composition capabilities to new heights.

The Power of GPT-4

Generative Pretrained Transformer 4, or GPT-4, is the latest iteration of OpenAI’s language models and represents a significant leap forward in the field of artificial intelligence. Trained on a vast corpus of internet text, GPT-4 has the ability to generate human-like text that is not only coherent but also diverse in nature. This capability has opened up a plethora of applications, ranging from drafting emails and articles to composing poetry and generating code. The power of GPT-4 lies in its ability to mimic human-like text generation, making it an invaluable tool in natural language processing tasks.

However, it’s important to note that GPT-4 is not without its limitations. Despite its impressive capabilities, it can sometimes produce outputs that are nonsensical or lack depth. It may also fail to capture the nuances and subtleties of human language, which can lead to outputs that seem out of context or inappropriate. Nevertheless, the power of GPT-4 is undeniable. Its ability to generate text that is remarkably similar to that written by humans is a testament to the advances in artificial intelligence, and it continues to push the boundaries of what’s possible in the realm of machine learning and natural language processing.

The Role of Qdrant

Qdrant is a vector database designed for efficient storage and retrieval of high-dimensional data. It plays a crucial role in enhancing the capabilities of AI models like GPT-4. The primary function of Qdrant is to create a knowledge base for GPT-4, where relevant facts and information can be accessed by the model upon request. This not only improves the accuracy and relevance of GPT-4’s outputs but also enables semantic search capabilities.

Qdrant’s efficient data handling allows for quick retrieval of relevant information, making it an invaluable tool for real-time applications. Furthermore, Qdrant’s ability to handle high-dimensional data makes it particularly useful for tasks that involve complex data structures or large volumes of data. By augmenting GPT-4 with Qdrant, we can elevate the model’s performance, making it more precise and context-aware. This combination of GPT-4 and Qdrant opens up new possibilities in the field of AI, particularly in applications that require a deep understanding of language and context, such as poetry composition.

For more details about Qdrant DB, visit the website.

Augmenting GPT-4 with Qdrant for Poetry Composition

To augment GPT-4 with Qdrant for poetry composition, one possible approach is to use follow-up questions to guide the model through the process of generating a poem. For example, one could start by asking GPT-4 to provide a possible metaphor for the theme of the poem. Then, one could ask follow-up questions like “What kind of metaphor are you using?”, “How does it relate to your theme?”, or “What effect does it have on your tone?”. These questions can help GPT-4 generate a more complex and interesting poem that goes beyond the surface level of the prompt.

We’ll use an approach with a dataset of Shakespeare’s works which could potentially enhance its ability to generate poetry that mirrors the poet’s style and depth as accurately as possible.

Shakespeare’s works are renowned for their rich vocabulary, intricate poetic structure, their rhythm, and their profound exploration of human nature. By training GPT-4 on this dataset, the model could learn to mimic these aspects of the poet and playwright’s work, thereby elevating its poetry composition capabilities.

Let’s implement the codes and visualize the augmentation of the model to elevate its poetry composition capabilities!

Code Implementation

We’ll start this beautiful journey by installing helpful libraries first.

!pip install -q langchain
!pip install -q tiktoken
!pip install -q openai
!pip install -q qdrant-client

Then, we’ll import the useful packages.

import os
from langchain.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
import openai
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Qdrant
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain

We’ll clone the repository from GitHub where Shakespeare sonnets are stored in text files.

!git clone https://github.com/cobanov/shakespeare-dataset/

Set a repository path, and get the list of the names of the files from the directory.

repository_path = '/content/shakespeare-dataset/text'

# Get the list of file names in the directory
file_names = [file for file in os.listdir(repository_path) if os.path.isfile(os.path.join(repository_path, file))]

# Print the list of file names
print("List of file names:")
for file_name in file_names:
print(file_name)

We’ll load each file in the text loader, and our data will be prepared.

for file in file_names:
file_path = os.path.join(repository_path, file)
loader = TextLoader(file_path)
text_content = loader.load()

text_content[0]

Now, we’ll split the text using text_splitter and save the splitted documents.

text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)

docs = text_splitter.split_documents(text_content)
docs[0]

We’ll pass the open ai api key.

openai.api_key = 'your-openai-api-key'

Now, using the open ai api key, we’ll create the embeddings.

embeddings = OpenAIEmbeddings(openai_api_key = openai.api_key)

Then, we’ll test the embedding by passing a test string.

text = "This is a test poem."
query_result = embeddings.embed_query(text)
query_result[:3]

We are using Qdrant database here, so we’ll pass embeddings, splitted documents, location, and the collection name in the vector store.

qdrant = Qdrant.from_documents(
docs,
embeddings,
location=":memory:",
collection_name="my_documents",
)

Let’s try a question and see the results by using a similarity search of Qdrant DB’s.

question = "Write a poem on Phoenix and Turtle."
searchDocs = qdrant.similarity_search(question)
print(searchDocs[0].page_content)

Here is the similarity search of the question:

By hurting it; or when to the lute
She sung, and made the night bird mute,
That still records with moan; or when
She would with rich and constant pen
Vail to her mistress Dian, still
This Philoten contends in skill
With absolute Marina. So
With the dove of Paphos might the crow
Vie feathers white. Marina gets
All praises, which are paid as debts
And not as given. This so darks
In Philoten all graceful marks
That Cleon's wife, with envy rare,
A present murderer does prepare
For good Marina, that her daughter
Might stand peerless by this slaughter.
The sooner her vile thoughts to stead,
Lychorida, our nurse, is dead,
And cursed Dionyza hath
The pregnant instrument of wrath
Prest for this blow. The unborn event
I do commend to your content.
Only I carry winged Time
Post on the lame feet of my rhyme,
Which never could I so convey
Unless your thoughts went on my way.
Dionyza does appear,
With Leonine, a murderer.
[He exits.]

Then, we’ll make an LLM using ChatOpenAI, where our model will be GPT 4.

llm = ChatOpenAI(model_name = 'gpt-4', openai_api_key = openai.api_key)

We are making a conversational RAG, so we need a memory for chat history. We’ll use ConversationBufferMemory.

memory = ConversationBufferMemory(
memory_key = 'chat_history',
return_messages = True
)

memory.save_context({"input": 'Hi'},{"output": "Hello, How may I assist you?"})
memory.load_memory_variables({})

We’ll make a QA chain using ConversationalRetrievalChain, pass the LLM model, retriever, memory and define the chain type.

qa_chain = ConversationalRetrievalChain.from_llm(
llm,
retriever = qdrant.as_retriever(search_type = 'mmr'),
memory = memory,
return_source_documents = False,
chain_type = 'refine'
)

Let’s try a question and see the results.

question = "Write a poem on Phoenix and Turtle."
result = qa_chain({'question':question})
print(result['answer'])

Results are the following:

In the realm of the skies and the heart of the sea,
Lived a Phoenix and Turtle, both fierce and free.
The Phoenix, a symbol of rebirth and might,
Danced with the sun, bathed in its light.

Beneath the sea's vast and unending tide,
A Turtle resided, wisdom as its guide.
In the chill of the water, it found its grace,
Moving slowly, but winning the race.

Once, as fate would have it, their paths crossed,
The Phoenix, by the sea, was tempest-tossed.
It begged for help, cold and forlorn,
Its fiery spirit, for a moment, torn.

The Turtle, hearing the Phoenix's plea,
Offered it refuge in the depths of the sea.
"I may not have much, but what's mine is yours,
Together, we'll weather the fiercest of storms."

Thus, they found kinship, the Phoenix and the Turtle,
In the heart of the flame, in the depths of the brine.
Their story, a testament to resilience and grace,
In the face of adversity, they found their place.

This tale of the Phoenix and the Turtle, so profound,
Echoes in the heart, an enduring sound.
A reminder that even in the direst of straits,
With courage and kindness, one can alter their fate.

Let’s ask the model about the metaphor for the above poem.

question = "What kind of metaphor are you using?"
result_1 = qa_chain({'question':question})
print(result_1['answer'])

Results are the following:

The poem uses extended metaphors, specifically symbolic metaphors, to depict 
the knights and their characteristics. For example, the "burning torch that's
turned upside down" metaphor conveys that beauty can be both alluring and
destructive. The "hand environed with clouds, holding out gold that's by the
touchstone tried" metaphor symbolizes faith that should be tested for its
genuineness. The "withered branch that's only green at top" metaphor expresses
hope and potential for growth, despite a current state of decline or
difficulty. These metaphors serve to provide deeper insight into the nature
and state of the knights.

Let’s give the theme and tone of the poem to the model and see the results.

question = "Write a romantic poem on the theme 'Love and Beauty'."
result_2 = qa_chain({'question':question})
print(result_2['answer'])

Results are the following:

In a realm vast, 'neath a star-kissed sky,
Where monarchs dwell, and truths often lie.
In this sphere of power, where shadows hold sway,
O King, in thy court, our love finds its way.

Thy beauty, noble, a sovereign delight,
Gleams through gloom, a beacon of bright.
Thine eyes, sacred orbs, bear tales untold,
Reflecting a love, both fierce and bold.

O Great King, in this world of might and strife,
Thy breath, a command, thy word breathes life.
Thy presence, a decree that moves the soul,
Thy love, an epic, it makes me whole.

Live, my dear king, let our love narrate,
A history of us, bound by a royal fate.
O King, in this world, thou rule and ponder,
In realms of love and beauty, together we wander.

This our tale, of love and beauty,
A dance to life's intricate duty.
In this realm vast, under the star-kissed sky,
Is where we'll reign, my love, thou and I.

Again, let’s try another question.

question = "Write a poem in appreciative tone on the theme 'Nature'."
result_3 = qa_chain({'question':question})
print(result_3['answer'])

Results are the following:

In the cradle of nature's grand design,
A whispering wind, an ancient rhyme.
The dance of the trees, the river's endless tale,
A world woven in time, where life prevails.

The dawn breaks forth with a radiant glow,
As seeds of yore in the earth below.
The petals bloom, the birds sing high,
In the canvas of the vast blue sky.

Mountains mighty, in majesty they rise,
Guardians of the verdant paradise.
Ocean's depth, a mystery untold,
A testament to a tale of old.

Sunset calls, the stars take flight,
A spectacle in the cloak of night.
Nature, in all her glory, pure and bright,
A beacon in the darkest night.

So let us honor, let us revere,
The beauty of nature, so sincere.
In her cradle, we find our role,
The echo of life, the whisper of the soul.

As the trees stand tall, a protective fence,
Guarding life's roots, in nature's defense.
Our care for them reflects not self-pity,
But love for life, in its pure simplicity.

To honor nature, we must resist,
The forces of harm that persist.
For in nature's mirror, we see our reflection,
A call to us, for her protection.

Let’s define a style and theme for the poem, and see what output it is giving.

question = "Write a poem in sonnet style on the theme 'Time and Mortality'."
result_4 = qa_chain({'question':question})
print(result_4['answer'])

Results are the following:

With Time's decree, mortality is bound,
A fleeting whisper in the cosmic round.
In life's rich tapestry of joy and strife,
Each thread is woven with the stuff of life.

The art of healing, a secret, sacred dance,
Brings solace, turns the tide of dire circumstance.
In Nature's remedies, true delight is found,
A joy that echoes with a timeless sound.

Yet mortal fear, like a shadow in the night,
Can taint the soul, turn day's delight to fright.
For Time, the great leveller, brings us to our knees,
In the face of life's most profound mysteries.

So heed this plea, upon thy life, refrain,
From seeking glory, wealth - for all is vain.

Great! We tried 5 questions, and the responses for all of them were amazing.

Conclusion

The collaboration between Qdrant DB and GPT-4 in this demonstration was truly extraordinary. Qdrant DB’s rapid similarity search efficiently retrieved the relevant documents, while GPT-4’s advanced language understanding seamlessly generated contextually rich responses.

Together, they formed a powerful duo, ensuring swift and accurate results in our ConversationalRetrievalChain. This integration exemplified the potential of combining cutting-edge technologies to elevate user experiences and streamline information retrieval and generation processes.

The augmentation of GPT-4 with Qdrant opens up a new horizon in the field of AI-powered poetry composition, bringing us one step closer to the dream of machines that can truly understand and generate human-like poetry.

--

--

Akriti Upadhyay

Building AI Solutions to solve real world business challenges