pendrag commited on
Commit
cd0cbb5
·
1 Parent(s): 29c8c51

updated google sdk calls

Browse files
Files changed (1) hide show
  1. app.py +115 -58
app.py CHANGED
@@ -18,6 +18,87 @@ GENAI_API = os.getenv("GENAI_API", "gemini")
18
 
19
  # LLM_MODEL_NAME must be set in the environment
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def search_inspire(query, size=10):
22
  """
23
  Search INSPIRE HEP database using fulltext search
@@ -80,16 +161,13 @@ def user_prompt(query, context):
80
 
81
  def llm_expand_query(query):
82
  """ Expands a query to variations of fulltext searches """
83
-
84
- response = client.chat.completions.create(
85
- model=MODEL_NAME,
86
- messages=[
87
- {
88
- "role": "user",
89
- "content": [
90
- {
91
- "type": "text",
92
- "text": f"""
93
  Expand this query into a the query format used for a fulltext search
94
  over the INSPIRE HEP database. Propose alternatives of the query to
95
  maximize the recall and join those variantes using OR operators and
@@ -110,36 +188,24 @@ def llm_expand_query(query):
110
  Query: {query}
111
 
112
  Expanded query:
113
- """
114
- }
115
- ]
116
- }
117
- ],
118
- response_format={
119
- "type": "text"
120
- },
121
- temperature=0,
122
- max_tokens=2048,
123
- top_p=1,
124
- frequency_penalty=0,
125
- presence_penalty=0
126
- )
127
 
128
- return response.choices[0].message.content
129
 
130
 
131
  def llm_generate_answer(prompt):
132
  """ Generate a response from the LLM """
133
-
134
- response = client.chat.completions.create(
135
- model=MODEL_NAME,
136
- messages=[
137
- {
138
- "role": "system",
139
- "content": [
140
- {
141
- "type": "text",
142
- "text": """You are part of a Retrieval Augmented Generation system
143
  (RAG) and are asked with a query and a context of results. Generate an
144
  answer substantiated by the results provided and citing them using
145
  their index when used to provide an answer text. Do not put two or more
@@ -149,30 +215,21 @@ def llm_generate_answer(prompt):
149
  summary of the previous discussed results. Do not consider results
150
  that are not related to the query and, if no specific answer can be
151
  provided, assert that in the brief answer."""
152
- }
153
- ]
154
- },
155
- {
156
- "role": "user",
157
- "content": [
158
- {
159
- "type": "text",
160
- "text": prompt
161
- }
162
- ]
163
- }
164
- ],
165
- response_format={
166
- "type": "text"
167
  },
168
- temperature=0,
169
- max_tokens=2048,
170
- top_p=1,
171
- frequency_penalty=0,
172
- presence_penalty=0
173
- )
174
-
175
- return response.choices[0].message.content
 
 
 
 
176
 
177
 
178
  def clean_refs(answer, results):
 
18
 
19
  # LLM_MODEL_NAME must be set in the environment
20
 
21
+ def _extract_text_from_message(message):
22
+ """Extract plain text from a message entry used in this codebase.
23
+
24
+ Messages in this project often look like:
25
+ {"role": "user", "content": [{"type": "text", "text": "..."}]}
26
+
27
+ This helper normalizes that shape to a single string.
28
+ """
29
+ content = message.get("content")
30
+ if isinstance(content, list) and len(content) > 0:
31
+ first = content[0]
32
+ if isinstance(first, dict) and "text" in first:
33
+ return first.get("text", "")
34
+ return str(first)
35
+ if isinstance(content, dict) and "text" in content:
36
+ return content.get("text", "")
37
+ if isinstance(content, str):
38
+ return content
39
+ return str(content)
40
+
41
+
42
+ def create_chat_response(messages, model, temperature=0, max_tokens=2048):
43
+ """Unified helper to produce a text response from either OpenAI or
44
+ Google's GenAI backends.
45
+
46
+ Returns a plain string with the assistant reply.
47
+ """
48
+ # OpenAI-style client: keep calling the same API
49
+ if GENAI_API == "openai":
50
+ response = client.chat.completions.create(
51
+ model=model,
52
+ messages=messages,
53
+ temperature=temperature,
54
+ max_tokens=max_tokens,
55
+ top_p=1,
56
+ frequency_penalty=0,
57
+ presence_penalty=0,
58
+ )
59
+ # Expect OpenAI-style response
60
+ try:
61
+ return response.choices[0].message.content
62
+ except Exception:
63
+ # Fallback: stringify
64
+ return str(response)
65
+
66
+ # Google GenAI path: convert messages to a single prompt and call
67
+ # the available model API (best-effort mapping).
68
+ prompt = "\n\n".join(f"{m.get('role','')}: {_extract_text_from_message(m)}" for m in messages)
69
+
70
+ # Try common modern GenAI SDK pattern: client.models.generate_content
71
+ try:
72
+ if hasattr(client, "models") and hasattr(client.models, "generate_content"):
73
+ # Use names similar to examples: contents and optional params
74
+ try:
75
+ resp = client.models.generate_content(model=model, contents=prompt, temperature=temperature, max_output_tokens=max_tokens)
76
+ except TypeError:
77
+ # Some versions may not accept those named args; try minimal call
78
+ resp = client.models.generate_content(model=model, contents=prompt)
79
+
80
+ # Response object often has `.text` or `.content`
81
+ text = getattr(resp, "text", None) or getattr(resp, "content", None)
82
+ if text is None:
83
+ return str(resp)
84
+ return text
85
+
86
+ # Older `google.generativeai` (legacy) had different surface; try a
87
+ # generous fallback: look for a top-level `generate` or `generate_text`.
88
+ if hasattr(client, "generate"):
89
+ resp = client.generate(prompt)
90
+ return getattr(resp, "text", str(resp))
91
+ if hasattr(client, "generate_text"):
92
+ resp = client.generate_text(prompt)
93
+ return getattr(resp, "text", str(resp))
94
+
95
+ except Exception as e:
96
+ # Surface the error with context to help debugging.
97
+ raise RuntimeError(f"GenAI model call failed: {e}")
98
+
99
+ raise RuntimeError("No suitable GenAI method found on `client`; please install/initialize supported SDK or set GENAI_API=openai")
100
+
101
+
102
  def search_inspire(query, size=10):
103
  """
104
  Search INSPIRE HEP database using fulltext search
 
161
 
162
  def llm_expand_query(query):
163
  """ Expands a query to variations of fulltext searches """
164
+ messages = [
165
+ {
166
+ "role": "user",
167
+ "content": [
168
+ {
169
+ "type": "text",
170
+ "text": f"""
 
 
 
171
  Expand this query into a the query format used for a fulltext search
172
  over the INSPIRE HEP database. Propose alternatives of the query to
173
  maximize the recall and join those variantes using OR operators and
 
188
  Query: {query}
189
 
190
  Expanded query:
191
+ """
192
+ }
193
+ ]
194
+ }
195
+ ]
 
 
 
 
 
 
 
 
 
196
 
197
+ return create_chat_response(messages=messages, model=MODEL_NAME, temperature=0, max_tokens=2048)
198
 
199
 
200
  def llm_generate_answer(prompt):
201
  """ Generate a response from the LLM """
202
+ messages = [
203
+ {
204
+ "role": "system",
205
+ "content": [
206
+ {
207
+ "type": "text",
208
+ "text": """You are part of a Retrieval Augmented Generation system
 
 
 
209
  (RAG) and are asked with a query and a context of results. Generate an
210
  answer substantiated by the results provided and citing them using
211
  their index when used to provide an answer text. Do not put two or more
 
215
  summary of the previous discussed results. Do not consider results
216
  that are not related to the query and, if no specific answer can be
217
  provided, assert that in the brief answer."""
218
+ }
219
+ ]
 
 
 
 
 
 
 
 
 
 
 
 
 
220
  },
221
+ {
222
+ "role": "user",
223
+ "content": [
224
+ {
225
+ "type": "text",
226
+ "text": prompt
227
+ }
228
+ ]
229
+ }
230
+ ]
231
+
232
+ return create_chat_response(messages=messages, model=MODEL_NAME, temperature=0, max_tokens=2048)
233
 
234
 
235
  def clean_refs(answer, results):