File size: 3,789 Bytes
4321589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#retrieval
def retrieve_documents (query, db, k=3):
    results = db.similarity_search(query, k=k)
    return results

# Context + Citation builder
def build_context(docs):
    context = ""
    sources = []

    for doc in docs:
        context += doc.page_content + "\n\n"

        source_info = f"{doc.metadata['source']} - page {doc.metadata['page']}"
        if source_info not in sources:
           sources.append(source_info)

    return context, sources

#LLM answer generator
from openai import OpenAI
from src.prompts import SYSTEM_PROMPT

client = OpenAI()

#def generate_answer(query, context, sources):
    #response = client.chat.completions.create(
        #model="gpt-4o-mini",
        #messages=[
           # {"role": "system", "content": SYSTEM_PROMPT},
           # {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query}"}
       # ]
 #   )
    
    #answer = response.choices[0].message.content

    #final_answer = answer + "\n\nSources:\n" + "\n".join(sources)

   # return final_answer

#Update Answer Generator
#Now modify generate_answer in:

#def generate_answer(query, context, sources, memory):
    #history_messages = format_chat_history(memory)

    #response = client.chat.completions.create(
        #model="gpt-4o-mini",
        #messages=[
            #{"role": "system", "content": SYSTEM_PROMPT},
            #*history_messages,
           # {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query}"}
       # ]
   # )

    #answer = response.choices[0].message.content

   # final_answer = answer + "\n\nSources:\n" + "\n".join(sources)

    #return final_answer

#Format Memory for LLM
#We need to convert memory into messages.
# So we Add this to rag_pipeline.py:
def format_chat_history(memory):
    messages = []

    for item in memory:
        messages.append({"role": "user", "content": item["user"]})
        messages.append({"role": "assistant", "content": item["bot"]})

    return messages

#Ticket github modification code
tools = [
    {
        "type": "function",
        "function": {
            "name": "create_support_ticket",
            "description": "Create a support ticket when user has an issue",
            "parameters": {
                "type": "object",
                "properties": {
                    "title": {"type": "string"},
                    "description": {"type": "string"}
                },
                "required": ["title", "description"]
            }
        }
    }
]

#updated answer_generator for ticketing
import json
from src.ticketing import create_github_issue

def generate_answer(query, context, sources, memory):
    history_messages = format_chat_history(memory)

    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            *history_messages,
            {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query}"}
        ],
        tools=tools,
        tool_choice="auto"
    )

    message = response.choices[0].message

    # If model decides to call function
    if message.tool_calls:
        tool_call = message.tool_calls[0]

        if tool_call.function.name == "create_support_ticket":
            args = json.loads(tool_call.function.arguments)

            issue_url = create_github_issue(
                title=args["title"],
                description=args["description"]
            )

            return f"✅ Support ticket created: {issue_url}"

    # Normal response
    answer = message.content
    final_answer = answer + "\n\nSources:\n" + "\n".join(sources)

    return final_answer