File size: 1,171 Bytes
9e97937
b985953
9e97937
 
 
 
 
 
622f41b
9e97937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from transformers import pipeline

# Load conversational model (small version for demo)
qa_pipeline = pipeline(
    "text-generation",
    model="microsoft/DialoGPT-medium",
    max_new_tokens=200
)

def generate_answer(question, context, search_results):
    # Build context-aware prompt
    background = "\n".join([
        f"Source {i+1}: {res['title']}\nContent: {res['content'][:1000]}"
        for i, res in enumerate(search_results)
    ])
    
    prompt = f"""
[Conversation History]
{context}

[Research Materials]
{background}

[User Question]
{question}

[Assistant Response Guidelines]
1. Answer conversationally like a helpful assistant
2. Acknowledge if previous context exists
3. Cite sources using numbers like [1], [2]
4. Be concise but comprehensive
5. If unsure, say so and suggest alternative approaches

Assistant:
"""
    # Generate response
    response = qa_pipeline(
        prompt,
        do_sample=True,
        temperature=0.7,
        top_k=50,
        pad_token_id=50256  # DialoGPT pad token
    )[0]['generated_text'].split("Assistant:")[-1].strip()
    
    return {
        "response": response,
        "sources": search_results
    }