-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
225 lines (191 loc) · 8.86 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import argparse
import os
from dotenv import load_dotenv
import warnings
import json
from config import initialize_env, create_config
import sys
# Import helper functions
from helpers.llm import get_llm
from helpers.agent import create_agent, update_llm_and_chain, initialize_session_state
from helpers.voice import voice_assistant, takeCommand, speak, transcribe_audio, cli_voice_assistant
from helpers.utils import generate_response, get_models, get_available_tools
# Import tools
from tools import *
# Load environment variables and suppress warnings
initialize_env()
def log_chat(user_input, answer):
"""Log chat messages to a file"""
try:
with open('chats.txt', 'a') as f:
f.write(f'\nHuman: {user_input}\n')
f.write(f'AI: {answer}\n')
except Exception as e:
print(f"Error logging chat: {str(e)}")
def initialize_cli_config():
"""Initialize configuration for CLI"""
try:
# Load configuration from config.txt
with open('config.txt', 'r') as f:
saved_config = json.load(f)
# Convert the saved config to our CLI format
config = {
'use_agent': saved_config.get('agent', False), # Use the 'agent' key from saved config
'use_voice': saved_config.get('voice', False),
'provider': saved_config.get('provider', "Google"),
'model': saved_config.get('model'),
'temperature': saved_config.get('temperature', 0.5),
'use_history': saved_config.get('history', True),
'history_size': saved_config.get('history_size', 5),
'selected_tools': saved_config.get('tools', []) # Use the 'tools' key from saved config
}
return config
except (FileNotFoundError, json.JSONDecodeError):
# If config file doesn't exist or is invalid, return default config
config = {
'use_agent': False,
'use_voice': False,
'provider': "Google",
'model': None,
'temperature': 0.5,
'use_history': True,
'history_size': 5,
'selected_tools': []
}
# Set default model based on provider
models = get_models(config['provider'])
config['model'] = models[0] if models else None
return config
def cli_interface(config):
"""Command Line Interface for the assistant"""
print("\nWelcome to Apsara 2.0 CLI - Advanced AI Assistant")
print("------------------------------------------------")
print(f"Current Configuration:")
print(f"LLM Provider: {config['provider']}")
print(f"Model: {config['model']}")
print(f"Agent Mode: {'Enabled' if config['use_agent'] else 'Disabled'}")
print(f"Voice Mode: {'Enabled' if config['use_voice'] else 'Disabled'}")
print("------------------------------------------------")
print("Commands:")
print("- 'exit' or 'quit' or 'bye': Exit the program")
print("- 'config': Change settings")
print("- 'voice': Toggle voice mode")
print("------------------------------------------------")
# Initialize LLM
llm = get_llm(temperature=config['temperature'],
provider=config['provider'],
model=config['model'])
# Initialize memory with different keys based on agent/chain
from langchain.memory import ConversationBufferWindowMemory
if config['use_agent']:
memory = ConversationBufferWindowMemory(k=config['history_size'],
return_messages=True,
memory_key='chat_history')
else:
memory = ConversationBufferWindowMemory(k=config['history_size'],
return_messages=True,
memory_key='chat_history')
# Initialize agent or chain
if config['use_agent']:
assistant = create_agent(llm, memory, config['selected_tools'])
else:
from helpers.llm import get_chain
assistant = get_chain(llm, memory)
while True:
try:
if config.get('use_voice', False):
query = cli_voice_assistant(config['use_agent'])
if query is None:
continue
print(f"You said: {query}")
else:
query = input("\nYou: ").strip()
# Check for exit commands first
if query.lower() in ['exit', 'quit', 'bye']:
print("Goodbye! Thank you for using Apsara 2.0")
return # This will exit the cli_interface function
if not query: # Skip empty inputs
continue
if query.lower() == 'config':
print("\nConfiguration Options:")
use_agent = input("Use Agent (yes/no)? ").lower().startswith('y')
# Update tools only if agent is enabled
if use_agent:
available_tools = get_available_tools()
print("\nAvailable tools:")
for i, tool in enumerate(available_tools, 1):
print(f"{i}. {tool}")
tool_input = input("\nEnter tool numbers to use (comma-separated, or 'all'): ").strip()
if tool_input.lower() == 'all':
config['selected_tools'] = available_tools
else:
try:
selected_indices = [int(i.strip()) - 1 for i in tool_input.split(',')]
config['selected_tools'] = [available_tools[i] for i in selected_indices if 0 <= i < len(available_tools)]
except (ValueError, IndexError):
print("Invalid input. No tools selected.")
config['selected_tools'] = []
else:
config['selected_tools'] = []
config['use_agent'] = use_agent
config['use_voice'] = input("Use Voice (yes/no)? ").lower().startswith('y')
# Reinitialize LLM and agent/chain based on new configuration
llm = get_llm(temperature=config['temperature'],
provider=config['provider'],
model=config['model'])
if config['use_agent']:
assistant = create_agent(llm, memory, config['selected_tools'])
else:
from helpers.llm import get_chain
assistant = get_chain(llm, memory)
continue
if query.lower() == 'voice':
config['use_voice'] = not config['use_voice']
print(f"Voice mode {'enabled' if config['use_voice'] else 'disabled'}")
continue
# Generate response
try:
if config['use_agent']:
response = assistant.invoke({"input": query})
answer = response['output']
else:
response = assistant.invoke({"question": query})
answer = response.get('response') or response.get('output', 'No response generated')
print(f"\nAI: {answer}")
# Log the conversation
log_chat(query, answer)
# Speak the response if voice mode is enabled
if config.get('use_voice', False):
speak(answer)
except Exception as e:
print(f"Error generating response: {str(e)}")
except KeyboardInterrupt:
print("\nOperation cancelled by user. Type 'exit' to quit.")
continue
except Exception as e:
print(f"\nAn error occurred: {str(e)}")
continue
def main():
parser = argparse.ArgumentParser(description="Apsara 2.0 - Advanced AI Assistant")
parser.add_argument("--gui", action="store_true", help="Launch the GUI version")
parser.add_argument("--config", action="store_true", help="Create or modify configuration")
args = parser.parse_args()
if args.gui:
# Launch Streamlit interface
import subprocess
subprocess.run(["streamlit", "run", "app.py"])
else:
try:
# Launch CLI interface
if args.config:
create_config()
config = initialize_cli_config()
cli_interface(config)
except KeyboardInterrupt:
print("\nGoodbye! Thank you for using Apsara 2.0")
except Exception as e:
print(f"An error occurred: {str(e)}")
finally:
sys.exit(0) # Ensure the program exits cleanly
if __name__ == "__main__":
main()