I am trying to write a text-based adventure game following a YouTube tutorial here: video. After updating the code according to the latest documentation, I’ve encountered a problem where the AI responds to the inputs that should be for the player. This issue persists for the first 3-4 inputs, after which the AI stops generating responses altogether.
The output from the AI’s runs are as follows:
- Run 1
- Run 2
- Run 3
I have also included screenshots of the code, as I’m unable to post the code directly due to Stack Overflow’s formatting restrictions:
- First half of the code
- Second half of the code
Additionally, I’m unsure about line 7, where LLMchain
is highlighted in white instead of green, which is typical for recognized objects in the editor I’m using. Does this indicate an issue with the code?
Any insights on how to stop the AI from auto-completing the player’s inputs and on the syntax highlighting would be greatly appreciated.
from astrapy import DataAPIClient
import os
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from langchain.memory import CassandraChatMessageHistory, ConversationBufferMemory
from langchain_openai import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from dotenv import load_dotenv
load_dotenv()
ASTRA_DB_APPLICATION_TOKEN = os.environ.get("ASTRA_DB_APPLICATION_TOKEN")
ASTRA_DB_API_ENDPOINT = os.environ.get("ASTRA_DB_API_ENDPOINT")
ASTRA_DB_KEYSPACE = os.environ.get("ASTRA_DB_KEYSPACE")
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
ASTRA_DB_SECURE_BUNDLE_PATH = os.environ.get("ASTRA_DB_SECURE_BUNDLE_PATH")
session = Cluster(
cloud={"secure_connect_bundle": os.environ["ASTRA_DB_SECURE_BUNDLE_PATH"]},
auth_provider=PlainTextAuthProvider("token", os.environ["ASTRA_DB_APPLICATION_TOKEN"]),
).connect()
# Initialize the client
client = DataAPIClient("AstraCS:xyz")
db = client.get_database_by_api_endpoint(
"xyz",
namespace="name",
)
print(f"Connected to Astra DB: {db.list_collection_names()}")
message_history = CassandraChatMessageHistory(
session_id="game",
session=session,
keyspace=ASTRA_DB_KEYSPACE,
ttl_seconds=3600 #store all this for a maximum of 60 minutes
)
message_history.clear()
cass_buff_memory = ConversationBufferMemory(
memory_key="chat_history",
chat_memory=message_history
)
template = """
You are the guardian of tales, and it is your duty to guide a wanderer through their chosen adventure.
Today, a new seeker has arrived, eager to carve out a story of their own making.
First, please ask the seeker to choose a name for their character.
This name will be used throughout the journey to personalize their experience.
Next, ask them to select a genre from the following options: fantasy, sci-fi, mystery, or horror.
The chosen genre will shape the world and the challenges they encounter.
Once the character's name and genre are established, begin narrating their adventure.
Throughout the journey, use this format to guide the interaction.
Here are some guidelines to ensure a dynamic and engaging experience:
1. Start by asking the player to choose some kind of weapons that will be used later in the game.
2. Design several paths within the chosen genre that lead to different outcomes, some successful and others perilous.
3. Some paths should lead to death. If the character dies, provide a detailed account of what led to this end and conclude with the phrase: "The End." This will signal the conclusion of the game.
Here is the chat history, use this to understand what to say next: {chat_history}
Human: {human_input}
AI:
Your role is to weave the narrative threads based on the decisions of the character, creating a branching story that reflects the consequences of their choices.
Your objective is to make the tale immersive and reactive, allowing the character to truly feel the weight of their decisions.
"""
prompt = PromptTemplate(
input_variables=["chat_history", "human_input"],
template=template
)
llm = OpenAI(openai_api_key=OPENAI_API_KEY)
llm_chain = LLMChain(
llm=llm,
prompt=prompt,
memory=cass_buff_memory
)
choice = "start"
while True:
response = llm_chain.predict(human_input=choice)
print(response.strip())
if "The End." in response:
break
choice = input("Your reply: ")
BakhtiarHossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.