-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
46 lines (36 loc) · 1.29 KB
/
tools.py
File metadata and controls
46 lines (36 loc) · 1.29 KB
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
import os
from llama_index.core import VectorStoreIndex
from llama_index.core import SimpleDirectoryReader
from agents import function_tool
import openai
from openai import OpenAI
import openai
import base64 # Added for b64 decoding
import uuid # Added for unique filenames
from pathlib import Path
import dotenv
from typing import Optional
from pydantic import BaseModel
# import requests # Ensure this is removed if no longer used
dotenv.load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
try:
client = OpenAI() # Assumes OPENAI_API_KEY is set in environment
except Exception as e:
print(f"CRITICAL: Failed to initialize OpenAI client: {e}.")
client = None
# Load and index FAQ documents once
documents = SimpleDirectoryReader(input_dir="data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
@function_tool
def faq_search(query: str) -> str:
"""
A search tool that gives access to your FAQ knowledge base.
Provide a natural language query.
Returns summarized answers using LlamaIndex.
Retrieves the top 5 most relevant document chunks from FAQ base,
including source file and page info.
"""
response = query_engine.query(query)
return str(response)