forked from heiko-braun/cai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent-cli.py
148 lines (111 loc) · 4.16 KB
/
agent-cli.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
import sys
from prompt_toolkit import PromptSession
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.styles import Style
from langchain.agents.openai_functions_agent.agent_token_buffer_memory import (
AgentTokenBufferMemory,
)
from langchain.callbacks.base import AsyncCallbackHandler
from langchain.schema import LLMResult
from langchain_core.messages import BaseMessage
from uuid import UUID
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, TypeVar, Union
from core.costs import TokenCostProcess, CostCalcAsyncHandler
from core.agent import agent_executor, agent_llm
import argparse
# ---
style = Style.from_dict(
{
"completion-menu.completion": "bg:#008888 #ffffff",
"completion-menu.completion.current": "bg:#00aaaa #000000",
"scrollbar.background": "bg:#88aaaa",
"scrollbar.button": "bg:#222222",
}
)
class CLIAsyncHandler(AsyncCallbackHandler):
def on_llm_start( self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any) -> None:
pass
async def on_llm_new_token(self, token: str, **kwargs) -> None:
pass
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
pass
async def on_tool_start(
self,
serialized: Dict[str, Any],
input_str: str,
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> None:
"""Run when tool starts running."""
tool_name = serialized["name"]
print(f"{tool_name} : {input_str}")
async def on_tool_end(
self,
output: str,
color: Optional[str] = None,
observation_prefix: Optional[str] = None,
llm_prefix: Optional[str] = None,
**kwargs: Any,
) -> None:
pass
async def on_chat_model_start(
self,
serialized: Dict[str, Any],
messages: List[List[BaseMessage]],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
"""Run when a chat model starts running."""
print("Thinking ...")
def main(args):
memory = AgentTokenBufferMemory(llm=agent_llm)
session = PromptSession(
lexer=None, completer=None, style=style
)
if(args.filename == None):
# enter QA loop
print("How can I help you?")
while True:
try:
prompt_text = session.prompt("> ")
except KeyboardInterrupt:
continue # Control-C pressed. Try again.
except EOFError:
break # Control-D pressed.
# request chat completion
try:
response_handle = agent_executor(
{"input": prompt_text, "history": memory.buffer},
callbacks=[CLIAsyncHandler()],
include_run_info=True,
)
memory.save_context({"input": prompt_text}, response_handle)
print(f"\n{response_handle['output']}")
except Exception as e:
print("Failed to call Openai API: ", str(e))
print("GoodBye!")
else:
# enter one-shot prompting from file
with open(args.filename) as f:
prompt = f.read()
prompt_text = prompt.replace('\n', ' ').replace('\r', '')
response_handle = agent_executor(
{"input": prompt_text, "history": []},
callbacks=[CLIAsyncHandler()],
include_run_info=True,
)
print(f"\n{response_handle['output']}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Camel Quickstart Assistant')
parser.add_argument('-f', '--filename', help='The input file that will be taken as a prompt', required=False)
args = parser.parse_args()
main(args)