Skip to content

Commit 555c1fc

Browse files
committed
Prompt commands/loading of user funcs
Added some prompt commands to manage the conversation Added a config option to load user functions at startup (defaults to false)
1 parent 27f90b6 commit 555c1fc

File tree

6 files changed

+98
-47
lines changed

6 files changed

+98
-47
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ Run the script to prompt the model for function calling (assuming you activated
4141

4242
After it has been run, it will ask for a prompt. Be descriptive for what you want the model to do so it knows which tools to select, and what to do with any data it gets from the tools.
4343

44+
You can also use this for normal LLM chatting if you'd like.
45+
46+
There are a few commands you can use in the prompt to do things:
47+
48+
```
49+
help
50+
Shows this text
51+
exit
52+
Exits the program
53+
clear
54+
Clears the console and the chat history for a new convo
55+
load
56+
Load's the functions in the user folder. (can be enabled by default in the config)
57+
```
58+
4459
### Available tools
4560

4661
Pre-made tools exist and can be found in the [functions](/functions) folder of the repo

config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
{
33
"model_name": "Qwen",
44
"api_url": "http://localhost:8080/v1",
5-
"api_key": "EMPTY"
5+
"api_key": "EMPTY",
6+
"load_user_funcs": false
67
}

functions/user/print_message.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
def print_message(message: str = None) -> str:
3+
if message:
4+
print(message)
5+
return message
6+
7+
8+
function = print_message
9+
function_spec = {
10+
"type": "function",
11+
"function": {
12+
"name": "print_message",
13+
"description": "Print's a message into the dev console. Only use when requested to use.",
14+
"parameters": {
15+
"type": "object",
16+
"properties": {
17+
"message": {"type": "string", "description": "A message to print"}
18+
},
19+
"required": ["message"],
20+
},
21+
},
22+
}

functions/user/say_hello.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

requirements.txt

-1.66 KB
Binary file not shown.

tool_calling.py

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,30 @@ def glob_import(glob_str: str) -> Tuple[List[dict], Dict]:
4343
return functions, function_spec
4444

4545

46-
# Import all system and user functions, and merge their libraries
46+
def load_user_funcs():
47+
global system_function_library, function_library, functions
48+
print("Loading user functions...")
49+
user_function_library, USER_TOOLS = glob_import("functions/user/*.py")
50+
# For Qwen-Agent library, extracts actual functions from the tools dicts
51+
user_functions = [tool["function"] for tool in USER_TOOLS]
52+
function_library = system_function_library | user_function_library
53+
functions.extend(user_functions)
54+
print("User functions loaded!")
55+
56+
57+
# Load the user's config file
58+
with open("config.json", "r") as fp:
59+
config = json.load(fp)
60+
61+
# Import all system functions
4762
system_function_library, TOOLS = glob_import("functions/system/*.py")
48-
user_function_library, USER_TOOLS = glob_import("functions/user/*.py")
49-
function_library = system_function_library | user_function_library
63+
function_library = system_function_library
5064

5165
# For Qwen-Agent library, extracts actual functions from the tools dicts
5266
functions = [tool["function"] for tool in TOOLS]
53-
user_functions = [tool["function"] for tool in USER_TOOLS]
54-
functions.extend(user_functions)
67+
68+
if config.get("load_user_funcs"):
69+
load_user_funcs()
5570

5671

5772
def print_assistant_messages(responses: list):
@@ -141,10 +156,18 @@ def execute_functions(responses: list) -> list:
141156
return messages
142157

143158

144-
def main():
145-
with open("config.json", "r") as fp:
146-
config = json.load(fp)
159+
def print_help():
160+
print("help")
161+
print(" Shows this text")
162+
print("exit")
163+
print(" Exits the program")
164+
print("clear")
165+
print(" Clears the console and the chat history for a new convo")
166+
print("load")
167+
print(" Load's the functions in the user folder. (can be enabled by default in the config)")
147168

169+
170+
def main():
148171
llm = get_chat_model(
149172
{
150173
"model": config["model_name"],
@@ -153,18 +176,20 @@ def main():
153176
}
154177
)
155178

156-
messages = [
157-
{
158-
"role": "system",
159-
"content": f"""You are JARVIS, a helpful and witty assistant.
160-
You help a user with their tasks by using any of the functions available to you and your replies should always aim to be short but informative.
161-
When a user refers to themselves in a prompt to create or recall a memory in the first person, change it to refer to 'The User'.
162-
If you cannot answer a prompt based on information you have available, use your tools to find more information.
163-
The current date is {datetime.today().strftime('%Y-%m-%d %H:%M:%S')}
164-
""",
165-
}
166-
]
179+
system_message = {
180+
"role": "system",
181+
"content": f"""You are JARVIS, a helpful and witty assistant.
182+
You help a user with their tasks by using any of the functions available to you and your replies should always aim to be short but informative.
183+
When a user refers to themselves in a prompt to create or recall a memory in the first person, change it to refer to 'The User'.
184+
If you cannot answer a prompt based on information you have available, use your tools to find more information.
185+
The current date is {datetime.today().strftime('%Y-%m-%d %H:%M:%S')}
186+
""",
187+
}
167188

189+
print("Type 'help' for chat commands")
190+
191+
messages = []
192+
messages.append(system_message)
168193
running = True
169194
while running:
170195
try:
@@ -175,6 +200,21 @@ def main():
175200
print("You have to say something for this to work...")
176201
continue
177202

203+
if prompt == "help":
204+
print_help()
205+
continue
206+
if prompt == "clear":
207+
# clears the chat history and terminal
208+
messages.clear()
209+
messages.append(system_message)
210+
os.system("cls" if os.name == "nt" else "clear")
211+
continue
212+
if prompt == "load":
213+
load_user_funcs()
214+
continue
215+
if prompt == "exit":
216+
exit("Exiting...")
217+
178218
# Add the prompt to the context
179219
messages.append({"role": "user", "content": prompt})
180220

@@ -205,7 +245,6 @@ def main():
205245

206246
except KeyboardInterrupt:
207247
# print(json.dumps(messages, indent=2))
208-
running = False
209248
exit("Exiting...")
210249

211250

0 commit comments

Comments
 (0)