Spaces:
Sleeping
Sleeping
Delete src
Browse files- src/__init__.py +0 -0
- src/chunking.py +0 -12
- src/loader.py +0 -27
- src/memory.py +0 -10
- src/prompts.py +0 -11
- src/rag_pipelines.py +0 -131
- src/ticketing.py +0 -24
- src/vector_store.py +0 -12
src/__init__.py
DELETED
|
File without changes
|
src/chunking.py
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 2 |
-
|
| 3 |
-
def chunk_documents(documents):
|
| 4 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
| 5 |
-
chunk_size=1000,
|
| 6 |
-
chunk_overlap=200,
|
| 7 |
-
separators=["\n\n", "\n", ".", " ", ""]
|
| 8 |
-
)
|
| 9 |
-
|
| 10 |
-
chunks = text_splitter.split_documents(documents)
|
| 11 |
-
|
| 12 |
-
return chunks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/loader.py
DELETED
|
@@ -1,27 +0,0 @@
|
|
| 1 |
-
from langchain_community.document_loaders import PyPDFLoader
|
| 2 |
-
import os
|
| 3 |
-
|
| 4 |
-
def load_all_pdfs(folder_path="data"):
|
| 5 |
-
documents = []
|
| 6 |
-
|
| 7 |
-
for file in os.listdir(folder_path):
|
| 8 |
-
if file.endswith(".pdf"):
|
| 9 |
-
loader = PyPDFLoader(os.path.join(folder_path, file))
|
| 10 |
-
documents.extend(loader.load())
|
| 11 |
-
|
| 12 |
-
return documents
|
| 13 |
-
|
| 14 |
-
from datasets import load_dataset
|
| 15 |
-
|
| 16 |
-
dataset = load_dataset("Rahbarnisa/RAG_docs", split="train")
|
| 17 |
-
|
| 18 |
-
import os
|
| 19 |
-
from datasets import load_dataset
|
| 20 |
-
|
| 21 |
-
dataset = load_dataset("Rahbarnisa/RAG_docs", split="train")
|
| 22 |
-
|
| 23 |
-
pdf_files = []
|
| 24 |
-
|
| 25 |
-
for item in dataset:
|
| 26 |
-
# depends on structure, but usually:
|
| 27 |
-
pdf_files.append(item["file"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/memory.py
DELETED
|
@@ -1,10 +0,0 @@
|
|
| 1 |
-
chat_history = []
|
| 2 |
-
|
| 3 |
-
def add_to_memory(user_input, bot_response):
|
| 4 |
-
chat_history.append({
|
| 5 |
-
"user": user_input,
|
| 6 |
-
"bot": bot_response
|
| 7 |
-
})
|
| 8 |
-
|
| 9 |
-
def get_memory():
|
| 10 |
-
return chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/prompts.py
DELETED
|
@@ -1,11 +0,0 @@
|
|
| 1 |
-
SYSTEM_PROMPT = """
|
| 2 |
-
You are a pharmaceutical employee support AI.
|
| 3 |
-
|
| 4 |
-
Rules
|
| 5 |
-
-Answer ONLY using the provided context
|
| 6 |
-
-Always include citations (file name + page)
|
| 7 |
-
-If information is missing, say: "I couldn't find his information"
|
| 8 |
-
-Be clear and professional
|
| 9 |
-
- If the user problem is not solved or unclear, suggest creating a support ticket
|
| 10 |
-
- If user agrees, call the function to create a ticket
|
| 11 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/rag_pipelines.py
DELETED
|
@@ -1,131 +0,0 @@
|
|
| 1 |
-
#retrieval
|
| 2 |
-
def retrieve_documents (query, db, k=3):
|
| 3 |
-
results = db.similarity_search(query, k=k)
|
| 4 |
-
return results
|
| 5 |
-
|
| 6 |
-
# Context + Citation builder
|
| 7 |
-
def build_context(docs):
|
| 8 |
-
context = ""
|
| 9 |
-
sources = []
|
| 10 |
-
|
| 11 |
-
for doc in docs:
|
| 12 |
-
context += doc.page_content + "\n\n"
|
| 13 |
-
|
| 14 |
-
source_info = f"{doc.metadata['source']} - page {doc.metadata['page']}"
|
| 15 |
-
if source_info not in sources:
|
| 16 |
-
sources.append(source_info)
|
| 17 |
-
|
| 18 |
-
return context, sources
|
| 19 |
-
|
| 20 |
-
#LLM answer generator
|
| 21 |
-
from openai import OpenAI
|
| 22 |
-
from src.prompts import SYSTEM_PROMPT
|
| 23 |
-
|
| 24 |
-
client = OpenAI()
|
| 25 |
-
|
| 26 |
-
#def generate_answer(query, context, sources):
|
| 27 |
-
#response = client.chat.completions.create(
|
| 28 |
-
#model="gpt-4o-mini",
|
| 29 |
-
#messages=[
|
| 30 |
-
# {"role": "system", "content": SYSTEM_PROMPT},
|
| 31 |
-
# {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query}"}
|
| 32 |
-
# ]
|
| 33 |
-
# )
|
| 34 |
-
|
| 35 |
-
#answer = response.choices[0].message.content
|
| 36 |
-
|
| 37 |
-
#final_answer = answer + "\n\nSources:\n" + "\n".join(sources)
|
| 38 |
-
|
| 39 |
-
# return final_answer
|
| 40 |
-
|
| 41 |
-
#Update Answer Generator
|
| 42 |
-
#Now modify generate_answer in:
|
| 43 |
-
|
| 44 |
-
#def generate_answer(query, context, sources, memory):
|
| 45 |
-
#history_messages = format_chat_history(memory)
|
| 46 |
-
|
| 47 |
-
#response = client.chat.completions.create(
|
| 48 |
-
#model="gpt-4o-mini",
|
| 49 |
-
#messages=[
|
| 50 |
-
#{"role": "system", "content": SYSTEM_PROMPT},
|
| 51 |
-
#*history_messages,
|
| 52 |
-
# {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query}"}
|
| 53 |
-
# ]
|
| 54 |
-
# )
|
| 55 |
-
|
| 56 |
-
#answer = response.choices[0].message.content
|
| 57 |
-
|
| 58 |
-
# final_answer = answer + "\n\nSources:\n" + "\n".join(sources)
|
| 59 |
-
|
| 60 |
-
#return final_answer
|
| 61 |
-
|
| 62 |
-
#Format Memory for LLM
|
| 63 |
-
#We need to convert memory into messages.
|
| 64 |
-
# So we Add this to rag_pipeline.py:
|
| 65 |
-
def format_chat_history(memory):
|
| 66 |
-
messages = []
|
| 67 |
-
|
| 68 |
-
for item in memory:
|
| 69 |
-
messages.append({"role": "user", "content": item["user"]})
|
| 70 |
-
messages.append({"role": "assistant", "content": item["bot"]})
|
| 71 |
-
|
| 72 |
-
return messages
|
| 73 |
-
|
| 74 |
-
#Ticket github modification code
|
| 75 |
-
tools = [
|
| 76 |
-
{
|
| 77 |
-
"type": "function",
|
| 78 |
-
"function": {
|
| 79 |
-
"name": "create_support_ticket",
|
| 80 |
-
"description": "Create a support ticket when user has an issue",
|
| 81 |
-
"parameters": {
|
| 82 |
-
"type": "object",
|
| 83 |
-
"properties": {
|
| 84 |
-
"title": {"type": "string"},
|
| 85 |
-
"description": {"type": "string"}
|
| 86 |
-
},
|
| 87 |
-
"required": ["title", "description"]
|
| 88 |
-
}
|
| 89 |
-
}
|
| 90 |
-
}
|
| 91 |
-
]
|
| 92 |
-
|
| 93 |
-
#updated answer_generator for ticketing
|
| 94 |
-
import json
|
| 95 |
-
from src.ticketing import create_github_issue
|
| 96 |
-
|
| 97 |
-
def generate_answer(query, context, sources, memory):
|
| 98 |
-
history_messages = format_chat_history(memory)
|
| 99 |
-
|
| 100 |
-
response = client.chat.completions.create(
|
| 101 |
-
model="gpt-4o-mini",
|
| 102 |
-
messages=[
|
| 103 |
-
{"role": "system", "content": SYSTEM_PROMPT},
|
| 104 |
-
*history_messages,
|
| 105 |
-
{"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query}"}
|
| 106 |
-
],
|
| 107 |
-
tools=tools,
|
| 108 |
-
tool_choice="auto"
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
message = response.choices[0].message
|
| 112 |
-
|
| 113 |
-
# If model decides to call function
|
| 114 |
-
if message.tool_calls:
|
| 115 |
-
tool_call = message.tool_calls[0]
|
| 116 |
-
|
| 117 |
-
if tool_call.function.name == "create_support_ticket":
|
| 118 |
-
args = json.loads(tool_call.function.arguments)
|
| 119 |
-
|
| 120 |
-
issue_url = create_github_issue(
|
| 121 |
-
title=args["title"],
|
| 122 |
-
description=args["description"]
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
return f"✅ Support ticket created: {issue_url}"
|
| 126 |
-
|
| 127 |
-
# Normal response
|
| 128 |
-
answer = message.content
|
| 129 |
-
final_answer = answer + "\n\nSources:\n" + "\n".join(sources)
|
| 130 |
-
|
| 131 |
-
return final_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/ticketing.py
DELETED
|
@@ -1,24 +0,0 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
|
| 3 |
-
GITHUB_TOKEN = "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"
|
| 4 |
-
REPO = "rahbarnisa/RAG-Capstone-Project"
|
| 5 |
-
|
| 6 |
-
def create_github_issue(title, description):
|
| 7 |
-
url = f"https://api.github.com/repos/{REPO}/issues"
|
| 8 |
-
|
| 9 |
-
headers = {
|
| 10 |
-
"Authorization": f"token {GITHUB_TOKEN}",
|
| 11 |
-
"Accept": "application/vnd.github.v3+json"
|
| 12 |
-
}
|
| 13 |
-
|
| 14 |
-
data = {
|
| 15 |
-
"title": title,
|
| 16 |
-
"body": description
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
response = requests.post(url, json=data, headers=headers)
|
| 20 |
-
|
| 21 |
-
if response.status_code == 201:
|
| 22 |
-
return response.json()["html_url"]
|
| 23 |
-
else:
|
| 24 |
-
return f"Error: {response.text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/vector_store.py
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
from langchain_community.vectorstores import FAISS
|
| 2 |
-
from langchain_openai import OpenAIEmbeddings
|
| 3 |
-
|
| 4 |
-
def create_vector_store(chunks):
|
| 5 |
-
embeddings = OpenAIEmbeddings()
|
| 6 |
-
|
| 7 |
-
db = FAISS.from_documents(
|
| 8 |
-
documents=chunks,
|
| 9 |
-
embedding=embeddings
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
return db
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|