hoshingakag commited on
Commit
9afac3f
β€’
1 Parent(s): cbcda9a

add tracking

Browse files
app.py CHANGED
@@ -1,9 +1,15 @@
1
  import os
2
  import time
 
 
3
  import gradio as gr
 
4
  import google.generativeai as genai
5
  from src.llamaindex_palm import LlamaIndexPaLM
6
 
 
 
 
7
  import logging
8
  logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p', level=logging.INFO)
9
  logger = logging.getLogger('llm')
@@ -15,6 +21,9 @@ llm.set_index_from_pinecone()
15
  # Credentials
16
  genai.configure(api_key=os.getenv('PALM_API_KEY'))
17
 
 
 
 
18
  # Gradio
19
  chat_history = []
20
 
@@ -23,31 +32,71 @@ def clear_chat() -> None:
23
  chat_history = []
24
  return None
25
 
 
 
 
 
 
 
 
 
26
  def generate_chat(prompt: str, llamaindex_llm: LlamaIndexPaLM):
27
  global chat_history
28
  # get chat history
29
- context_chat_history = "\n".join(chat_history)
30
 
31
  logger.info("Generating Message...")
32
  logger.info(f"User Message:\n{prompt}\n")
33
  chat_history.append(prompt)
34
 
 
 
 
 
 
 
 
 
 
 
35
  # get context
36
  context_from_index = llamaindex_llm.generate_response(prompt)
37
  logger.info(f"Context from Llama-Index:\n{context_from_index}\n")
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  prompt_with_context = f"""
40
- Rule:
41
  You are in a role play of Gerard Lee and you need to pretend to be him to answer questions from people who interested in Gerard's background.
42
- Respond in 2 to 3 complete sentences, unless specifically asked by the user to elaborate on something. Use only the History and Context to inform your answers.
43
- --
44
- History:
45
  {context_chat_history}
46
- --
47
- Context:
48
  {context_from_index}
49
- --
50
- User Query:
51
  {prompt}
52
  """
53
 
@@ -62,13 +111,40 @@ def generate_chat(prompt: str, llamaindex_llm: LlamaIndexPaLM):
62
  ]
63
  )
64
  result = response.result
 
 
 
 
 
65
 
66
  except Exception as e:
67
  result = "Seems something went wrong. Please try again later."
68
  logger.error(f"Exception {e} occured\n")
 
69
 
70
  chat_history.append(result)
71
  logger.info(f"Bot Message:\n{result}\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  return result
73
 
74
  with gr.Blocks() as app:
@@ -76,7 +152,7 @@ with gr.Blocks() as app:
76
  bubble_full_width=False,
77
  container=False,
78
  show_share_button=False,
79
- avatar_images=[None, './akag-g-only.png']
80
  )
81
  with gr.Row():
82
  msg = gr.Textbox(
@@ -90,7 +166,7 @@ with gr.Blocks() as app:
90
  send = gr.Button(
91
  value="",
92
  variant="primary",
93
- icon="./send-message.png",
94
  scale=1
95
  )
96
 
 
1
  import os
2
  import time
3
+ import datetime
4
+
5
  import gradio as gr
6
+
7
  import google.generativeai as genai
8
  from src.llamaindex_palm import LlamaIndexPaLM
9
 
10
+ import wandb
11
+ from wandb.sdk.data_types.trace_tree import Trace
12
+
13
  import logging
14
  logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p', level=logging.INFO)
15
  logger = logging.getLogger('llm')
 
21
  # Credentials
22
  genai.configure(api_key=os.getenv('PALM_API_KEY'))
23
 
24
+ # W&B
25
+ wandb.init(project="ChatExp")
26
+
27
  # Gradio
28
  chat_history = []
29
 
 
32
  chat_history = []
33
  return None
34
 
35
+ def get_chat_history(chat_history) -> str:
36
+ ind = 0
37
+ formatted_chat_history = ""
38
+ for message in chat_history:
39
+ formatted_chat_history += f"User: \n{message}\n" if ind % 2 == 0 else f"Bot: \n{message}\n"
40
+ ind += 1
41
+ return formatted_chat_history
42
+
43
  def generate_chat(prompt: str, llamaindex_llm: LlamaIndexPaLM):
44
  global chat_history
45
  # get chat history
46
+ context_chat_history = "\n".join(list(filter(None, chat_history)))
47
 
48
  logger.info("Generating Message...")
49
  logger.info(f"User Message:\n{prompt}\n")
50
  chat_history.append(prompt)
51
 
52
+ # w&b trace start
53
+ start_time_ms = round(datetime.datetime.now().timestamp() * 1000)
54
+
55
+ root_span = Trace(
56
+ name="LLMChain",
57
+ kind="chain",
58
+ start_time_ms=start_time_ms,
59
+ metadata={"user": "Gradio"},
60
+ )
61
+
62
  # get context
63
  context_from_index = llamaindex_llm.generate_response(prompt)
64
  logger.info(f"Context from Llama-Index:\n{context_from_index}\n")
65
 
66
+ # w&b trace agent
67
+ agent_end_time_ms = round(datetime.datetime.now().timestamp() * 1000)
68
+ agent_span = Trace(
69
+ name="Agent",
70
+ kind="agent",
71
+ status_code="success",
72
+ metadata={
73
+ "framework": "Llama-Index",
74
+ "index_type": "VectorStoreIndex",
75
+ "vector_store": "Pinecone",
76
+ "model_name": "models/text-bison-001",
77
+ "temperture": 0.7,
78
+ "top_k": 40,
79
+ "top_p": 0.95,
80
+ },
81
+ start_time_ms=start_time_ms,
82
+ end_time_ms=agent_end_time_ms,
83
+ inputs={"query": prompt},
84
+ outputs={"response": context_from_index},
85
+ )
86
+ root_span.add_child(agent_span)
87
+
88
  prompt_with_context = f"""
89
+ [System]
90
  You are in a role play of Gerard Lee and you need to pretend to be him to answer questions from people who interested in Gerard's background.
91
+ Respond the User Query below in no more than 5 complete sentences, unless specifically asked by the user to elaborate on something. Use only the History and Context to inform your answers.
92
+
93
+ [History]
94
  {context_chat_history}
95
+
96
+ [Context]
97
  {context_from_index}
98
+
99
+ [User Query]
100
  {prompt}
101
  """
102
 
 
111
  ]
112
  )
113
  result = response.result
114
+ success_flag = "success"
115
+ if result is None:
116
+ result = "Seems something went wrong. Please try again later."
117
+ logger.error(f"Result with 'None' received\n")
118
+ success_flag = "fail"
119
 
120
  except Exception as e:
121
  result = "Seems something went wrong. Please try again later."
122
  logger.error(f"Exception {e} occured\n")
123
+ success_flag = "fail"
124
 
125
  chat_history.append(result)
126
  logger.info(f"Bot Message:\n{result}\n")
127
+
128
+ # w&b trace llm
129
+ llm_end_time_ms = round(datetime.datetime.now().timestamp() * 1000)
130
+ llm_span = Trace(
131
+ name="LLM",
132
+ kind="llm",
133
+ status_code=success_flag,
134
+ start_time_ms=agent_end_time_ms,
135
+ end_time_ms=llm_end_time_ms,
136
+ inputs={"input": prompt_with_context},
137
+ outputs={"result": result},
138
+ )
139
+ root_span.add_child(llm_span)
140
+
141
+ # w&b finalize trace
142
+ root_span.add_inputs_and_outputs(
143
+ inputs={"query": prompt}, outputs={"result": result}
144
+ )
145
+ root_span._span.end_time_ms = llm_end_time_ms
146
+ root_span.log(name="llm_app_trace")
147
+
148
  return result
149
 
150
  with gr.Blocks() as app:
 
152
  bubble_full_width=False,
153
  container=False,
154
  show_share_button=False,
155
+ avatar_images=[None, './asset/akag-g-only.png']
156
  )
157
  with gr.Row():
158
  msg = gr.Textbox(
 
166
  send = gr.Button(
167
  value="",
168
  variant="primary",
169
+ icon="./asset/send-message.png",
170
  scale=1
171
  )
172
 
akag-g-only.png β†’ asset/akag-g-only.png RENAMED
File without changes
send-message.png β†’ asset/send-message.png RENAMED
File without changes