mudassir032 commited on
Commit
7eb93c1
·
verified ·
1 Parent(s): f676892

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_groq import ChatGroq
2
+ import streamlit as st
3
+ llm = ChatGroq(temperature=0.7,
4
+
5
+ model_name="llama-3.3-70b-versatile", api_key="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")
6
+
7
+ from crewai import Agent,Crew
8
+
9
+ class NarratorAgent(Agent):
10
+ def act(self, level):
11
+ return f"Welcome to level:{level}, prepare to face your next challenge"
12
+
13
+ class QuestionAgent(Agent):
14
+ def act(self, level,history):
15
+ return llm.invoke(f"Generate a general knowledge questions for a Q and A game, the player is at level:{level}, only generate the question not the answer and those quesstions which are not present in the history:{history}")
16
+
17
+ class HintAgent(Agent):
18
+ def act(self, level,question):
19
+ return llm.invoke(f"Generate some hints regarding this question:{question}, the player is at level:{level} so dont reveal too many details")
20
+
21
+ class AnswerCheckerAgent(Agent):
22
+ def act(self, question, answer):
23
+ return llm.invoke(f"Check if the user answer:{answer} is correct to the question:{question}, Only say exact words yes or no")
24
+
25
+ narrator = NarratorAgent(
26
+ role="Narrator",
27
+ goal="Guide the story forward",
28
+ backstory="The narrator knows all details of the world and its characters."
29
+ )
30
+ questioner = QuestionAgent(
31
+ role="Questioner",
32
+ goal="Generate a general knowledge question",
33
+ backstory="The questioner has all general knowledge about the world"
34
+ )
35
+
36
+ hinter = HintAgent(
37
+ role="Hinter",
38
+ goal="Generate some hints about the question",
39
+ backstory="The hinter has knowledge about the question, and knows the correct answer"
40
+ )
41
+ validator = AnswerCheckerAgent(
42
+ role="Validator",
43
+ goal="Check if the answer is correct",
44
+ backstory="The validator has all knowledge about the question, and knows the correct answer"
45
+ )
46
+ st.title("Knowledge Dungeouns")
47
+ def main():
48
+ level = 1
49
+ history = []
50
+ while level < 11:
51
+
52
+ points = 0
53
+ description = narrator.act(level)
54
+ question = questioner.act(level,history).content
55
+ hint = hinter.act(level,question)
56
+ history.append(question)
57
+ st.write(f"{description}\n{question}")
58
+ #answer = input(f"{description}\n{question}")
59
+ answer = st.text_input("Enter your answer",key='user answer')
60
+ if st.button("Submit Answer"):
61
+ if answer.lower().strip() == "hint":
62
+ st.write(f"Hint: {hint.content}")
63
+ else:
64
+ response = validator.act(question, answer).content.lower().strip()
65
+ st.write("Answer Checker Response:", response)
66
+
67
+ if response in ["yes.", "yes", "yes. ", "yes ", " yes"]:
68
+ level += 1
69
+ st.success(f"Correct! Moving to Level {level}")
70
+ else:
71
+ st.error("Game Over")
72
+ st.write("Correct Answer:", llm.invoke(f"{question}").content)
73
+ return
74
+ main()