-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_reader.py
More file actions
116 lines (90 loc) · 4.87 KB
/
Copy pathdocument_reader.py
File metadata and controls
116 lines (90 loc) · 4.87 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
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
"""
Document loader for the Dynamic RAG Bot.
Standalone document reader that extracts information with source citations.
"""
import os
import instructor
from openai import OpenAI
from dotenv import load_dotenv
from models import DocumentResponse, Source
# Load environment variables
load_dotenv()
# Create instructor-patched OpenAI client
client = instructor.from_openai(OpenAI(api_key=os.getenv("OPENAI_API_KEY")))
def load_document(country: str, question: str) -> DocumentResponse:
"""
Load country document and extract information relevant to the question.
Args:
country: Country name (e.g., "france")
question: Full user question (e.g., "Tell me about French wine regions")
Returns:
DocumentResponse with extracted content and source citations
"""
# Construct file path
file_path = f"data/countries/{country.lower()}.md"
try:
# Read the markdown file
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Add line numbers for citation tracking
lines = content.split('\n')
numbered_content = '\n'.join([f"{i+1:3d}: {line}" for i, line in enumerate(lines)])
# Create extraction prompt with clear instructions
extraction_prompt = f"""You are a document extraction specialist. Think step by step to extract information from this country document.
DOCUMENT: {country.lower()}.md
{numbered_content}
USER QUESTION: {question}
THINKING STEPS:
1. What exactly is the user asking about?
2. Does this document contain information that directly answers this question?
3. If YES: Extract the specific relevant information with citations
4. If NO: State that the information is not available in this document
EXTRACTION RULES:
- Be super targeted to the exact question asked
- Only include information that directly answers the question
- If the document doesn't have the specific information, say so clearly
- Do NOT suggest related topics or provide general information
- Do NOT add follow-up suggestions
- Provide accurate source citations with exact line numbers
CITATION FORMAT:
file="{country.lower()}.md", section="[SectionName]", line_start=[number], line_end=[number], quote="[exact text]"
EXAMPLES:
Example 1 - Wine Regions Question:
Question: "Tell me about French wine regions"
Good response: "France has several renowned wine regions including Bordeaux, known for red wines, Burgundy famous for Pinot Noir and Chardonnay, and Champagne which produces the famous sparkling wine. Each region has unique terroir and grape varieties." [with specific citations]
Bad response: "France has wine regions. France also has great cuisine and cultural attractions you might enjoy..." [too broad, suggesting other topics]
Example 2 - Economy Question:
Question: "What is Germany's main industry?"
Good response: "Germany's economy is driven by strong sectors including aerospace (Airbus), automotive (BMW, Mercedes), and manufacturing." [with citations]
Bad response: "Germany has a strong economy with various industries. Germany also has rich culture and history..." [not focused]
Example 3 - Information Not Found:
Question: "What is France's space program?"
Good response: "This document does not contain information about France's space program."
Bad response: "While I don't have space program information, France has a strong aerospace industry with Airbus..." [suggesting alternatives instead of being direct]
Example 4 - Tourist Attractions:
Question: "What are the main tourist attractions in Japan?"
Good response: "Japan's main tourist attractions include traditional temples, Mount Fuji, and modern cities like Tokyo with their unique blend of ancient and contemporary culture." [with citations]
Bad response: "Japan has many attractions. Japan also has interesting food culture and technology..." [too scattered]
REMEMBER: Answer only what is specifically asked. If not found, say so directly. No suggestions or related topics."""
# Use instructor to get structured extraction
response = client.chat.completions.create(
model="gpt-5-mini",
response_model=DocumentResponse,
messages=[
{"role": "system", "content": extraction_prompt},
{"role": "user", "content": f"Extract information to answer: {question}"}
]
)
return response
except FileNotFoundError:
# Handle case where country file doesn't exist
return DocumentResponse(
content=f"I don't have detailed information about {country.title()} in my knowledge base. The country document was not found.",
sources=[]
)
except Exception as e:
# Handle other errors
return DocumentResponse(
content=f"I encountered an error while searching for information about {country.title()}: {str(e)}",
sources=[]
)