-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
91 lines (78 loc) · 2.82 KB
/
main.py
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
# src/mcp_llm_bridge/main.py
import os
import asyncio
from dotenv import load_dotenv
from mcp import StdioServerParameters
from mcp_llm_bridge.config import BridgeConfig, LLMConfig
from mcp_llm_bridge.bridge import BridgeManager
import colorlog
import logging
handler = colorlog.StreamHandler()
handler.setFormatter(colorlog.ColoredFormatter(
"%(log_color)s%(levelname)s%(reset)s: %(cyan)s%(name)s%(reset)s - %(message)s",
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
},
secondary_log_colors={},
style='%'
))
logger = colorlog.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
async def main():
# Load environment variables
load_dotenv()
# Get the project root directory (where test.db is located)
project_root = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
db_path = os.path.join(project_root, "test.db")
# Define paths for PDF processing
template_path = os.path.join(project_root, "tests", "template.pdf")
output_dir = os.path.join(project_root, "tests", "filled")
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
# Configure bridge
config = BridgeConfig(
mcp_server_params=StdioServerParameters(
command="uvx",
args=["mcp-server-sqlite", "--db-path", db_path],
env=None
),
llm_config=LLMConfig(
api_key=os.getenv("OPENAI_API_KEY"),
model=os.getenv("OPENAI_MODEL", "gpt-4o"),
base_url=None
),
system_prompt=f"""You are a helpful assistant that can use tools to help answer questions.
You can:
1. Query the database using SQL
2. Fill PDF forms by specifying coordinates and text
For PDF operations, you'll need:
- Template path: {template_path}
- Output directory: {output_dir}
- Coordinates (x, y) for each text field
- The text content to fill"""
)
logger.info(f"Starting bridge with model: {config.llm_config.model}")
logger.info(f"Using database at: {db_path}")
# Use bridge with context manager
async with BridgeManager(config) as bridge:
while True:
try:
user_input = input("\nEnter your prompt (or 'quit' to exit): ")
if user_input.lower() in ['quit', 'exit', 'q']:
break
response = await bridge.process_message(user_input)
print(f"\nResponse: {response}")
except KeyboardInterrupt:
logger.info("\nExiting...")
break
except Exception as e:
logger.error(f"\nError occurred: {e}")
if __name__ == "__main__":
asyncio.run(main())