I just posted one bot output - and I have then pointed out what it made wrong.
this works…
Ok, I must admit that adding that in memory cache can’t work… since the script is restarted with a fresh empty cache each time… so from here on redis should be used or at least a cache file…
or just for demonstration we could run the function twice with the same prompt
from openai import OpenAI
client = OpenAI()
# A simple in-memory cache for demonstration purposes
cache = {}
def check_cache(prompt):
"""Checks if the response for the given prompt is cached."""
if prompt in cache:
print("Cache hit")
return cache[prompt]
print("Cache miss")
return None
def call_openai_api(prompt):
"""Calls the OpenAI API to get a response for the given prompt."""
# Check if the response is cached
cached_response = check_cache(prompt)
if cached_response is not None:
return cached_response
# If not cached, make an API call
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
temperature=1,
max_tokens=300,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
# Extract the response text
response_text = response.choices[0].message.content.strip()
# Cache the response for future use
cache[prompt] = response_text
return response_text
if __name__ == "__main__":
user_prompt = 'write a poem'
response_text = call_openai_api(user_prompt)
print(response_text)
response_text = call_openai_api(user_prompt)
print(response_text)
and here we go:
(venv) (base) jochen@dev1:~/projects/tests$ python openaitest.py
Cache miss
Upon the canvas of a starlit night,
Dancing dreams twirl in the pale moon’s light.
Billowed whispers of the weeping willows,
Silent symphony on feathered pillows.
Stars akin to diamonds, scattered across the velvet sky,
Illuminating stories that were never by and by.
Lost in the cosmic ballet, in universe we twine,
Heartbeat whispers echo through the corridors of time.
Against a backdrop of infinity, ceaselessly we roam,
In search of fleeting moments, we dare to call our own.
Under the watchful eyes of wandering constellations,
Spinning tales of love, life, and quiet contemplations.
Sapphire seas roaring with tales untold,
Reflecting vibrant stories of the sun, bold.
Dressed in the golden hue of the morning sun,
Daylight awakes, and nighttime is done.
The sun stands proudly, lord of the azure above,
While flowers bloom, an ode to life’s undying love.
Witness to the eternal loop of day and night,
The solitary moon glistens, bathing the world in silver light.
The poet weaves words into grand tapestries,
Heartstrings strumming to love’s enchanting melodies.
From life, a poem emerges, quiet and profound,
In every breath, a whisper of beauty found.
Cache hit <<<< that worked
Upon the canvas of a starlit night,
Dancing dreams twirl in the pale moon’s light.
Billowed whispers of the weeping willows,
Silent symphony on feathered pillows.
Stars akin to diamonds, scattered across the velvet sky,
Illuminating stories that were never by and by.
Lost in the cosmic ballet, in universe we twine,
Heartbeat whispers echo through the corridors of time.
Against a backdrop of infinity, ceaselessly we roam,
In search of fleeting moments, we dare to call our own.
Under the watchful eyes of wandering constellations,
Spinning tales of love, life, and quiet contemplations.
Sapphire seas roaring with tales untold,
Reflecting vibrant stories of the sun, bold.
Dressed in the golden hue of the morning sun,
Daylight awakes, and nighttime is done.
The sun stands proudly, lord of the azure above,
While flowers bloom, an ode to life’s undying love.
Witness to the eternal loop of day and night,
The solitary moon glistens, bathing the world in silver light.
The poet weaves words into grand tapestries,
Heartstrings strumming to love’s enchanting melodies.
From life, a poem emerges, quiet and profound,
In every breath, a whisper of beauty found.