PierreBrunelle commited on
Commit
34bee60
·
verified ·
1 Parent(s): 737d90e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -30
app.py CHANGED
@@ -35,6 +35,21 @@ def create_prompt(top_k_list: list[dict], question: str) -> str:
35
  QUESTION:
36
  {question}'''
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  def validate_token(token):
39
  try:
40
  api = HfApi()
@@ -45,7 +60,6 @@ def validate_token(token):
45
 
46
  def process_files(token, pdf_files, chunk_limit, chunk_separator):
47
  if not validate_token(token):
48
-
49
  return "Invalid token. Please enter a valid Hugging Face token."
50
 
51
  # Initialize Pixeltable
@@ -54,9 +68,11 @@ def process_files(token, pdf_files, chunk_limit, chunk_separator):
54
 
55
  # Create a table to store the uploaded PDF documents
56
  t = pxt.create_table(
57
- 'chatbot_demo.documents',
58
- {'document': pxt.DocumentType(nullable=True),
59
- 'question': pxt.StringType(nullable=True)}
 
 
60
  )
61
 
62
  # Insert the PDF files into the documents table
@@ -86,51 +102,34 @@ def process_files(token, pdf_files, chunk_limit, chunk_separator):
86
  .limit(5)
87
  )
88
 
89
- # Add computed columns to the table for context retrieval and prompt creation
90
  t['question_context'] = chunks_t.queries.top_k(t.question)
91
- t['prompt'] = create_prompt(
92
- t.question_context, t.question
93
- )
94
-
95
- # Prepare messages for the API
96
- msgs = [
97
- {
98
- 'role': 'system',
99
- 'content': 'Answer questions using only the provided context. If the context lacks sufficient information, state this clearly.'
100
- },
101
- {
102
- 'role': 'user',
103
- 'content': t.prompt
104
- }
105
- ]
106
-
107
- # Add OpenAI response column
108
  t['response'] = openai.chat_completions(
109
  model='gpt-4o-mini-2024-07-18',
110
- messages=msgs,
111
  max_tokens=300,
112
  top_p=0.9,
113
  temperature=0.7
114
  )
115
-
116
- # Extract the answer text from the API response
117
  t['gpt4omini'] = t.response.choices[0].message.content
118
 
119
  return "Files processed successfully. You can start the discussion."
120
 
121
  def get_answer(token, msg):
122
  if not validate_token(token):
123
-
124
  return "Invalid token. Please enter a valid Hugging Face token."
125
 
126
  t = pxt.get_table('chatbot_demo.documents')
127
- chunks_t = pxt.get_table('chatbot_demo.chunks')
128
-
129
  # Insert the question into the table
130
  t.insert([{'question': msg}])
131
-
 
132
  answer = t.select(t.gpt4omini).where(t.question == msg).collect()['gpt4omini'][0]
133
-
134
  return answer
135
 
136
  def respond(token, message, chat_history):
 
35
  QUESTION:
36
  {question}'''
37
 
38
+ # New UDF for creating messages
39
+ @pxt.udf
40
+ def create_messages(prompt: str) -> list[dict]:
41
+ """Creates a structured message list for the LLM from the prompt"""
42
+ return [
43
+ {
44
+ 'role': 'system',
45
+ 'content': 'Answer questions using only the provided context. If the context lacks sufficient information, state this clearly.'
46
+ },
47
+ {
48
+ 'role': 'user',
49
+ 'content': prompt
50
+ }
51
+ ]
52
+
53
  def validate_token(token):
54
  try:
55
  api = HfApi()
 
60
 
61
  def process_files(token, pdf_files, chunk_limit, chunk_separator):
62
  if not validate_token(token):
 
63
  return "Invalid token. Please enter a valid Hugging Face token."
64
 
65
  # Initialize Pixeltable
 
68
 
69
  # Create a table to store the uploaded PDF documents
70
  t = pxt.create_table(
71
+ 'chatbot_demo.documents',
72
+ {
73
+ 'document': pxt.DocumentType(nullable=True),
74
+ 'question': pxt.StringType(nullable=True)
75
+ }
76
  )
77
 
78
  # Insert the PDF files into the documents table
 
102
  .limit(5)
103
  )
104
 
105
+ # Add computed columns to create the chain of transformations
106
  t['question_context'] = chunks_t.queries.top_k(t.question)
107
+ t['prompt'] = create_prompt(t.question_context, t.question)
108
+ t['messages'] = create_messages(t.prompt) # New computed column for messages
109
+
110
+ # Add the response column using the messages computed column
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  t['response'] = openai.chat_completions(
112
  model='gpt-4o-mini-2024-07-18',
113
+ messages=t.messages, # Use the computed messages column
114
  max_tokens=300,
115
  top_p=0.9,
116
  temperature=0.7
117
  )
 
 
118
  t['gpt4omini'] = t.response.choices[0].message.content
119
 
120
  return "Files processed successfully. You can start the discussion."
121
 
122
  def get_answer(token, msg):
123
  if not validate_token(token):
 
124
  return "Invalid token. Please enter a valid Hugging Face token."
125
 
126
  t = pxt.get_table('chatbot_demo.documents')
127
+
 
128
  # Insert the question into the table
129
  t.insert([{'question': msg}])
130
+
131
+ # The answer will be automatically generated through the chain of computed columns
132
  answer = t.select(t.gpt4omini).where(t.question == msg).collect()['gpt4omini'][0]
 
133
  return answer
134
 
135
  def respond(token, message, chat_history):