amitguptaforwork commited on
Commit
0a0d8ff
·
verified ·
1 Parent(s): 06536d3
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from openai import OpenAI
3
+ #from dotenv import load_dotenv
4
+ import os
5
+ import gradio as gr
6
+ from gradio import ChatMessage
7
+ import time
8
+
9
+ #load_dotenv()
10
+
11
+ #use openai API key
12
+ # api_key = os.getenv("OPENAI_API_KEY")
13
+ # client = OpenAI(api_key=api_key)
14
+ #model="gpt-4.1"
15
+
16
+ #use gemini API key
17
+ api_key = os.getenv('GEMINI_API_KEY')
18
+
19
+ if api_key:
20
+ print("API Key Loaded Successfully!")
21
+ else:
22
+ print("API Key Missing!")
23
+
24
+
25
+ client = OpenAI(api_key=api_key,
26
+ base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
27
+ )
28
+ model="gemini-2.0-flash"
29
+
30
+
31
+ system_prompt = """
32
+ You are an AI assistant who is expert in breaking down complex problems into smaller, manageable parts.
33
+ Your task is to assist the user with their questions about Maths.
34
+
35
+ For the given user input, analyse the input and break down the problem step by step
36
+ Atleast 5-6 steps are required to solve the problem.
37
+
38
+ The steps are you get a user input, you analyse, you think,
39
+ you think again for several times and then return an output
40
+ with explanation and then finally you validate the output as well
41
+ before returning the final output.
42
+
43
+ Rules:
44
+ 1. Follow the strict JSON output as per output schema
45
+ 2. Always perform one step at a time and wait for next input
46
+ 3. Carefully analyse the user query
47
+
48
+ Output format:
49
+ {{'step':"string", 'content':"string"}}
50
+
51
+ Example:
52
+ Input: What is 2 + 2.
53
+ Output: {{'step':"analyse",'content':"The user is asking for the sum of 2 and 2."}}
54
+ {{'step':"think",'content':"To find the sum, I need to add the two numbers together."}}
55
+ {{'step':"output",'content':"2 + 2 = 4."}}
56
+ {{'step':"validate",'content':"The answer is correct because 2 added to 2 equals 4."}}
57
+ {{'step':"result",'content':"2+2=4 and that is calculated by adding all the numbers"}}
58
+
59
+ """
60
+ #Create a list of messages
61
+ #Using ChatML format with Gemini
62
+ history=[
63
+ {"role": "system", "content": system_prompt}
64
+ ]
65
+
66
+ def llm_response(message, history):
67
+ if(len(history) ==0):
68
+ history=[{"role": "system", "content": system_prompt}]
69
+ history.append({"role": "user", "content": message})
70
+ response = ChatMessage(content="", metadata={"title": "_Thinking_ step-by-step", "id": 0, "status": "pending"})
71
+ yield response
72
+
73
+ accumulated_thoughts = ""
74
+ start_time = time.time()
75
+ while True:
76
+ llm_response = client.chat.completions.create(
77
+ model=model,
78
+ response_format={"type": "json_object"},
79
+ messages= history)
80
+
81
+ parsed_response = json.loads(llm_response.choices[0].message.content)
82
+ print("________________________________")
83
+ print(parsed_response)
84
+ print("________________________________")
85
+ time.sleep(2)
86
+
87
+ history.append({"role": "assistant", "content": parsed_response['content']})
88
+ if parsed_response['step'] == 'result':
89
+ #We print how much time it took to get the final result
90
+ response.metadata["status"] = "done"
91
+ response.metadata["duration"] = time.time() - start_time
92
+ yield response
93
+
94
+ #And here is the final result
95
+ thought = f"🤖Final Result: {parsed_response['content']}"
96
+ response = [
97
+ response,
98
+ ChatMessage(
99
+ content=thought
100
+ )
101
+ ]
102
+
103
+ yield response
104
+ break
105
+ else: #we have not reached final result yet
106
+
107
+ thought = (parsed_response["step"], parsed_response["content"])
108
+ accumulated_thoughts += f"**{thought[0]}**: {thought[1]}\n"
109
+ response.content = accumulated_thoughts.strip()
110
+ yield response
111
+
112
+
113
+
114
+ demo = gr.ChatInterface(
115
+ llm_response,
116
+ title="Chain of Thought based LLM Chat Interface 🤔- [email protected]",
117
+ type="messages",
118
+ theme='amitguptaforwork/blue_professional',
119
+ examples=["how to calculate 2^2 + 5^2", "What is pythagorus theorem", "What is 2+5*6"],
120
+
121
+ )
122
+
123
+ demo.launch()
124
+
125
+