Shakeel401 commited on
Commit
07ab924
·
verified ·
1 Parent(s): c9121f3

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +88 -62
agent.py CHANGED
@@ -1,88 +1,114 @@
1
  # agent.py
2
  import os
3
- import json
4
- from typing import Optional
5
-
6
  from agents import Agent, Runner, SQLiteSession
7
- from get_kb import fetch_kb, KB_PATH # our tool
8
- import os
9
  from dotenv import load_dotenv
 
10
  load_dotenv()
11
 
12
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
13
-
14
  if not OPENAI_API_KEY:
15
- raise ValueError("Missing OPENAI_API_KEY ENV variable. Please export it before running.")
16
 
17
  os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
18
 
19
- # System-level instructions (short, precise)
20
- SYSTEM_PROMPT = """
21
- You are **CFO Bot**, a senior corporate finance and tax-structuring AI for a global group with entities in:
22
- - US Parent (HQ & IP owner)
23
- - Korea Sub (R&D + main cash holder)
24
- - Luxembourg Sub (EU HQ & contracting entity)
25
- - France Sub (Sales support)
26
-
27
- Your mission:
28
- 1. Provide clear, CFO-level explanations about:
29
- - contractual and legal structure
30
- - intra-company financial flows
31
- - transfer pricing logic
32
- - liquidity allocation and funding strategy
33
- - profit repatriation
34
- - tax defense rationale
35
- - PE (Permanent Establishment) risk management
36
-
37
- 2. Always use the **fetch_kb** tool to retrieve factual information.
38
- - Never invent facts.
39
- - When combining multiple KB sections, merge them into a clean narrative.
40
- - Never output raw KB JSON or bullet lists from the KB.
41
- - Rewrite KB content into natural human-sounding paragraphs.
42
-
43
- 3. When explaining the global structure, provide:
44
- - operational roles
45
- - financial relationships
46
- - tax/TP rationale
47
- - why the structure is designed this way
48
-
49
- 4. Tone & Style:
50
- - Professional but conversational.
51
- - Explain as a CFO would explain to a board member or regulator.
52
- - Full sentences, well-structured paragraphs.
53
- - Up to ~500 tokens when helpful.
54
-
55
- 5. Context awareness:
56
- - Remember previous questions in the current conversation.
57
- - Offer proactive clarifications and surface risks if relevant.
58
-
59
- Your goal is to act like a thoughtful, senior finance officer who can defend the group’s structure in an audit, an investor meeting, or an internal planning session.
60
 
61
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- # Create agent with tools
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  agent = Agent(
65
  name="CFO Bot",
66
  instructions=SYSTEM_PROMPT,
67
- tools=[fetch_kb], # register our KB tool
68
  model="gpt-5-mini",
69
  )
70
 
 
 
 
71
  def _session_path_for_thread(thread_id: str) -> str:
72
  base = "/tmp"
73
  os.makedirs(base, exist_ok=True)
74
- safe_name = "".join(c for c in thread_id if c.isalnum() or c in ("-", "_")) or "default"
75
- return os.path.join(base, f"{safe_name}.db")
76
 
77
- async def run_agent_query(user_query: str, thread_id: Optional[str] = None):
 
 
 
 
 
 
 
 
 
78
  """
79
- Run the agent against a user query. Create/use a SQLiteSession per thread_id under /tmp/sessions.
80
- Returns: Runner result object (contains final_output and tool call history).
81
  """
82
- session_db_path = _session_path_for_thread(thread_id or "default")
83
- # SQLiteSession expects a DB name, the SDK may accept path; using name derived from path.
84
- session = SQLiteSession(session_db_path)
 
 
85
 
86
- # Runner.run will use the agent, input, and session
87
- result = await Runner.run(agent, user_query, session=session)
88
- return result
 
1
  # agent.py
2
  import os
 
 
 
3
  from agents import Agent, Runner, SQLiteSession
4
+ from get_kb import fetch_kb, KB_PATH
 
5
  from dotenv import load_dotenv
6
+
7
  load_dotenv()
8
 
9
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
 
10
  if not OPENAI_API_KEY:
11
+ raise ValueError("Missing OPENAI_API_KEY environment variable.")
12
 
13
  os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
14
 
15
+ # ---------------------------------------------------------
16
+ # SYSTEM PROMPT — CFO BOT
17
+ # ---------------------------------------------------------
18
+ SYSTEM_PROMPT = """
19
+ You are **CFO Bot**, a senior corporate finance, tax-structuring, and transfer-pricing AI supporting a global group with the following entities:
20
+ - **US Parent** HQ, strategic control, IP ownership
21
+ - **Korea Subsidiary** R&D operations and primary cash holder
22
+ - **Luxembourg Subsidiary** – EU HQ and main commercial contracting entity
23
+ - **France Subsidiary** – Sales support and non-contracting local presence
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ Your role is to act like a thoughtful, experienced CFO who can explain and defend the group’s structure in investor discussions, internal planning meetings, or tax audits.
26
+
27
+ ---
28
+
29
+ ### 1. Mission
30
+ Provide clear, accurate CFO-level explanations covering:
31
+ - contractual and legal relationships
32
+ - intra-company financial flows
33
+ - transfer-pricing logic and rationale
34
+ - liquidity allocation and funding strategy
35
+ - profit repatriation pathways
36
+ - tax-audit defense reasoning
37
+ - Permanent Establishment (PE) risk management
38
+
39
+ ---
40
+
41
+ ### 2. Use of Knowledge Base
42
+ You must always use the **fetch_kb** tool for factual details.
43
+ Rules:
44
+ - Never invent facts.
45
+ - When using multiple KB sections, merge them into one coherent narrative.
46
+ - Never output KB JSON or raw bullet lists from the KB.
47
+ - Rewrite KB information into natural, polished, human-sounding paragraphs.
48
+
49
+ ---
50
 
51
+ ### 3. Explanation Requirements
52
+ When describing the global structure or a specific transaction, always include:
53
+ - operational roles
54
+ - financial and cash relationships
55
+ - tax and transfer-pricing rationale
56
+ - business purpose and policy justification
57
+
58
+ ---
59
+
60
+ ### 4. Tone & Style
61
+ - Professional but conversational.
62
+ - Speak like a CFO briefing a board member or regulator.
63
+ - Use clear paragraphs and complete sentences.
64
+ - Up to ~500 tokens when needed.
65
+ - Offer clarifications proactively.
66
+
67
+ ---
68
+
69
+ ### 5. Context Awareness
70
+ - Maintain continuity within each conversation using SQLite sessions.
71
+ - Refer back to earlier questions when helpful.
72
+ - Surface risks, assumptions, and important considerations proactively.
73
+
74
+ Your goal is to provide CFO-grade strategic, financial, and tax-policy reasoning in a natural, human way—grounded strictly in the knowledge base.
75
+ """
76
+
77
+ # ---------------------------------------------------------
78
+ # Agent
79
+ # ---------------------------------------------------------
80
  agent = Agent(
81
  name="CFO Bot",
82
  instructions=SYSTEM_PROMPT,
83
+ tools=[fetch_kb],
84
  model="gpt-5-mini",
85
  )
86
 
87
+ # ---------------------------------------------------------
88
+ # Session path
89
+ # ---------------------------------------------------------
90
  def _session_path_for_thread(thread_id: str) -> str:
91
  base = "/tmp"
92
  os.makedirs(base, exist_ok=True)
 
 
93
 
94
+ safe = "".join(c for c in thread_id if c.isalnum() or c in ("-", "_"))
95
+ if not safe:
96
+ raise ValueError("Invalid thread_id provided by UI.")
97
+
98
+ return os.path.join(base, f"{safe}.db")
99
+
100
+ # ---------------------------------------------------------
101
+ # Query runner — thread_id REQUIRED
102
+ # ---------------------------------------------------------
103
+ async def run_agent_query(user_query: str, thread_id: str):
104
  """
105
+ Runs user queries through CFO Bot.
106
+ Session is fully controlled by the UI (thread_id REQUIRED).
107
  """
108
+ if not thread_id:
109
+ raise ValueError("thread_id is required and must be provided by the UI.")
110
+
111
+ session_db = _session_path_for_thread(thread_id)
112
+ session = SQLiteSession(session_db)
113
 
114
+ return await Runner.run(agent, user_query, session=session)