Spaces:
Runtime error
Runtime error
Update llm.py
Browse files
llm.py
CHANGED
@@ -1,10 +1,48 @@
|
|
1 |
-
from transformers import
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
def generate_answer(context,
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
|
3 |
+
# Load conversational model (small version for demo)
|
4 |
+
qa_pipeline = pipeline(
|
5 |
+
"text-generation",
|
6 |
+
model="microsoft/DialoGPT-medium",
|
7 |
+
max_new_tokens=200
|
8 |
+
)
|
9 |
|
10 |
+
def generate_answer(question, context, search_results):
|
11 |
+
# Build context-aware prompt
|
12 |
+
background = "\n".join([
|
13 |
+
f"Source {i+1}: {res['title']}\nContent: {res['content'][:1000]}"
|
14 |
+
for i, res in enumerate(search_results)
|
15 |
+
])
|
16 |
+
|
17 |
+
prompt = f"""
|
18 |
+
[Conversation History]
|
19 |
+
{context}
|
20 |
+
|
21 |
+
[Research Materials]
|
22 |
+
{background}
|
23 |
+
|
24 |
+
[User Question]
|
25 |
+
{question}
|
26 |
+
|
27 |
+
[Assistant Response Guidelines]
|
28 |
+
1. Answer conversationally like a helpful assistant
|
29 |
+
2. Acknowledge if previous context exists
|
30 |
+
3. Cite sources using numbers like [1], [2]
|
31 |
+
4. Be concise but comprehensive
|
32 |
+
5. If unsure, say so and suggest alternative approaches
|
33 |
+
|
34 |
+
Assistant:
|
35 |
+
"""
|
36 |
+
# Generate response
|
37 |
+
response = qa_pipeline(
|
38 |
+
prompt,
|
39 |
+
do_sample=True,
|
40 |
+
temperature=0.7,
|
41 |
+
top_k=50,
|
42 |
+
pad_token_id=50256 # DialoGPT pad token
|
43 |
+
)[0]['generated_text'].split("Assistant:")[-1].strip()
|
44 |
+
|
45 |
+
return {
|
46 |
+
"response": response,
|
47 |
+
"sources": search_results
|
48 |
+
}
|