AI-based Text Adventure Game Autocompleting Player Inputs Prematurely

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: ")

New contributor

BakhtiarHossain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật