Examples
Styled Generation

Styled Generation

The HyperChat™ RAG API is not just good for asking questions to knowledge bases, it can also be used to do "styled generation", i.e., write like a certain person or in a certain style. Let's do Shakespeare as an example. Although it's likely that our LLMs know how to write like Shakespeare all too well, the example just hopes to demonstrate the use of a few components of our system for similar use cases.

We will first build a Collection of Shakespeare's works, upload it to the Platform, build a prompt to mimic Shakespeare's "tone of voice" while generating a keynote and then chat with this bot to write in style.

Get Data

For this purpose, let's pull the following data into text files:

These texts will simply serve as stylistic references rather than information sources since we will tell the LLM to do so.

Create Collection

We then go to the Platform page (opens in a new tab) to create the Shakespearean Collection. See the Quickstart page (opens in a new tab) for more info on how to do this.

Run RAG

The scenario we're going with is a John Doe giving a keynote speech at a defense technologies conference in the style of Shakespeare! We've prepared the prompt to force the LLM to do this by adding the titles of the 4 documents we added to the Collections to the prompt. Note that we haven't made any mentions of Shakespeare in the prompt, so the model is gathering the style purely from the documents.

Here's the example code:

# pip install --upgrade hyperbee
import os
from hyperbee import HyperBee
 
client    = HyperBee(api_key=os.environ["HYPERBEE_API_KEY"], base_url="https://api-rag.hyperbee.ai/v1")
 
def hyperchat_keynotegenerator(nameofperson, nameofevent, subjectmatter, namespace):
	llm_in = f"""I would like you to prepare the keynote speech text for {nameofperson} at {nameofevent}, but written in a different style! 
	I am sending you some writing excerpts as part of the *documents*, specifically, 
	- "SCENE I. Rousillon. The COUNT's palace.", 
	- "SCENE I. London. The palace.", 
	- "Sonnet I" and 
	- "SCENE I. Alexandria. A room in CLEOPATRA's palace.". 
 
	Look at these writings, and adapt the keynote speech writing style accordingly. 
	For the content of the speech you will prepare, you should take into account the *subject matter* I will convey below. 
	You should only take the communication tone and style as an example from the documents I provide, not the content.
 
	Subject Matter: {subjectmatter}
 
	Now prepare the keynote:"""
 
	messages = [{"role": "user", "content": llm_in}]
	response = client.chat.completions.create(model="hyperchat", optimization="premium", messages=messages, stream=False, extra_body={"namespace": namespace})
 
	return response.choices[0].message.content
 
shakespearean_keynote = hyperchat_keynotegenerator(nameofperson  = "John Doe", 
									 			   nameofevent   = "EUROSATORY 2024 Conference for Defense Technologies", 
									 			   subjectmatter = "Regulations on space-based satellite weaponry and controlling space debris.", 
									 			   namespace     = "defd3ac0-f0b3-4b99-8024-69bf519f7686")
print(shakespearean_keynote)

Hint: the Shakespeare namespace used in this example is public, you can use the Collection ID as is in your examples

Example output:

	Ladies and gentlemen, esteemed colleagues, and noble guests,

	In this grand assembly, we gather under the auspices of innovation and vigilance, to deliberate upon the celestial realm that now beckons our stewardship. The heavens, once the domain of dreams and distant stars, have become the stage upon which our earthly endeavors now play.

	As we stand at the precipice of this new frontier, we are tasked with the solemn duty of crafting regulations that shall govern the use of space-based satellite weaponry. Let us not be swayed by the allure of unchecked power, for in the vast expanse above, our actions echo with the weight of responsibility. We must ensure that our reach into the stars is guided by wisdom and foresight, lest we sow the seeds of discord among the constellations.

	Moreover, the specter of space debris looms large, a testament to our past follies. It is incumbent upon us to devise measures that shall cleanse the firmament of these remnants, ensuring that the pathways of the cosmos remain unblemished for future generations. Let us not be the architects of our own downfall, but rather the custodians of a legacy that shall endure beyond the bounds of time.

	In this endeavor, let us be united, as the stars themselves are bound by the tapestry of the night sky. Let our discourse be marked by clarity and purpose, as we chart a course towards a future where the heavens are a sanctuary of peace and progress.

	With these thoughts, I implore you to lend your voices and your wisdom to this noble cause, for the decisions we make today shall resonate through the ages, shaping the destiny of our world and the worlds beyond.

	Thank you.