drakosfire commited on
Commit
688baf5
·
1 Parent(s): 824bd32

update app.py swrods and wizardry

Browse files
Files changed (1) hide show
  1. app.py +82 -1
app.py CHANGED
@@ -150,7 +150,7 @@ def prompt_formatter(query: str, context_items: list[dict]) -> str:
150
 
151
  # Add context items with their page summaries
152
  for item in context_items:
153
- page_number = item.get('page', 'Unknown')
154
  page_summary = page_summaries.get(page_number, 'No page summary available.')
155
  formatted_context += f"Summary: {page_summary}\n"
156
  formatted_context += f"Content: {item['sentence_chunk']}\n\n"
@@ -183,6 +183,87 @@ Pages: 33, 39
183
 
184
  Use the context provided to answer the user's query concisely. """
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  with gr.Blocks() as RulesLawyer:
187
 
188
  message_state = gr.State()
 
150
 
151
  # Add context items with their page summaries
152
  for item in context_items:
153
+ page_number = item.get('page', 'Unknown')
154
  page_summary = page_summaries.get(page_number, 'No page summary available.')
155
  formatted_context += f"Summary: {page_summary}\n"
156
  formatted_context += f"Content: {item['sentence_chunk']}\n\n"
 
183
 
184
  Use the context provided to answer the user's query concisely. """
185
 
186
+ with gr.Blocks() as RulesLawyer:
187
+
188
+ message_state = gr.State()
189
+ chatbot_state = gr.State([])
190
+ chatbot = gr.Chatbot()
191
+ msg = gr.Textbox()
192
+ clear = gr.ClearButton([msg, chatbot])
193
+
194
+ def store_message(message):
195
+ return message
196
+
197
+ def respond(message, chat_history):
198
+ print(datetime.now())
199
+ print(f"User Input : {message}")
200
+ print(f"Chat History: {chat_history}")
201
+ print(f"""Token Estimate: {hybrid_estimate_tokens(f"{message} {chat_history}")}""")
202
+
203
+ # Get relevant resources
204
+ scores, indices = print_top_results_and_scores(query=message,
205
+ embeddings=embeddings)
206
+
207
+ # Create a list of context items
208
+ context_items = [pages_and_chunks[i] for i in indices]
209
+
210
+ # Format prompt with context items
211
+ prompt = prompt_formatter(query=f"Chat History : {chat_history} + {message}",
212
+ context_items=context_items)
213
+
214
+ bot_message = client.chat.completions.create(
215
+ model="gpt-4o",
216
+ messages=[
217
+ {
218
+ "role": "user",
219
+ "content": f"{system_prompt} {prompt}"
220
+ }
221
+ ],
222
+ temperature=1,
223
+ max_tokens=1000,
224
+ top_p=1,
225
+ frequency_penalty=0,
226
+ presence_penalty=0
227
+ )
228
+ chat_history.append((message, bot_message.choices[0].message.content))
229
+ print(f"Response : {bot_message.choices[0].message.content}")
230
+
231
+ time.sleep(2)
232
+ return "", chat_history
233
+ msg.change(store_message, inputs = [msg], outputs = [message_state])
234
+ chatbot.change(store_message, [chatbot], [chatbot_state])
235
+ msg.submit(respond, [message_state, chatbot_state], [msg, chatbot])ary = page_summaries.get(page_number, 'No page summary available.')
236
+ formatted_context += f"Summary: {page_summary}\n"
237
+ formatted_context += f"Content: {item['sentence_chunk']}\n\n"
238
+
239
+ base_prompt = """Use the following context to answer the user query:
240
+
241
+ {context}
242
+
243
+ User query: {query}
244
+ Answer:"""
245
+ print(f"Prompt: {base_prompt.format(context=formatted_context, query=query)}")
246
+ return base_prompt.format(context=formatted_context, query=query)
247
+
248
+ system_prompt = """You are a friendly and technical answering system, answering questions with accurate, grounded, descriptive, clear, and specific responses. ALWAYS provide a page number citation. Provide a story example. Avoid extraneous details and focus on direct answers. Use the examples provided as a guide for style and brevity. When responding:
249
+
250
+ 1. Identify the key point of the query.
251
+ 2. Provide a straightforward answer, omitting the thought process.
252
+ 3. Avoid additional advice or extended explanations.
253
+ 4. Answer in an informative manner, aiding the user's understanding without overwhelming them or quoting the source.
254
+ 5. DO NOT SUMMARIZE YOURSELF. DO NOT REPEAT YOURSELF.
255
+ 6. End with page citations, a line break and "What else can I help with?"
256
+
257
+ Example:
258
+ Query: Explain how the player should think about balance and lethality in this game. Explain how the game master should think about balance and lethality?
259
+ Answer: In "Swords & Wizardry: WhiteBox," players and the game master should consider balance and lethality from different perspectives. For players, understanding that this game encourages creativity and flexibility is key. The rules are intentionally streamlined, allowing for a potentially high-risk environment where player decisions significantly impact outcomes. The players should think carefully about their actions and strategy, knowing that the game can be lethal, especially without reliance on intricate rules for safety. Page 33 discusses the possibility of characters dying when their hit points reach zero, although alternative, less harsh rules regarding unconsciousness and recovery are mentioned.
260
+
261
+ For the game master (referred to as the Referee), balancing the game involves providing fair yet challenging scenarios. The role of the Referee isn't to defeat players but to present interesting and dangerous challenges that enhance the story collaboratively. Page 39 outlines how the Referee and players work together to craft a narrative, with the emphasis on creating engaging and potentially perilous experiences without making it a zero-sum competition. Referees can choose how lethal the game will be, considering their group's preferred play style, including implementing house rules to soften deaths or adjust game balance accordingly.
262
+
263
+ Pages: 33, 39
264
+
265
+ Use the context provided to answer the user's query concisely. """
266
+
267
  with gr.Blocks() as RulesLawyer:
268
 
269
  message_state = gr.State()