forked from gptscript-ai/py-gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgptscript.py
234 lines (190 loc) · 8.58 KB
/
gptscript.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
226
227
228
229
230
231
232
233
234
import json
import os
import platform
from subprocess import Popen, PIPE
from sys import executable
from time import sleep
from typing import Any, Callable, Awaitable, List
import requests
from gptscript.confirm import AuthResponse
from gptscript.credentials import Credential, to_credential
from gptscript.frame import RunFrame, CallFrame, PromptFrame, Program
from gptscript.opts import GlobalOptions
from gptscript.prompt import PromptResponse
from gptscript.run import Run, RunBasicCommand, Options
from gptscript.text import Text
from gptscript.tool import ToolDef, Tool
class GPTScript:
__gptscript_count = 0
__server_url = ""
__process: Popen = None
__server_ready: bool = False
def __init__(self, opts: GlobalOptions = None):
if opts is None:
opts = GlobalOptions()
self.opts = opts
GPTScript.__gptscript_count += 1
if GPTScript.__server_url == "":
GPTScript.__server_url = os.environ.get("GPTSCRIPT_URL", "127.0.0.1:0")
if GPTScript.__gptscript_count == 1 and os.environ.get("GPTSCRIPT_DISABLE_SERVER", "") != "true":
self.opts.toEnv()
GPTScript.__process = Popen(
[_get_command(), "--listen-address", GPTScript.__server_url, "sdkserver"],
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
env={e.split("=", 1)[0]: e.split("=", 1)[1] for e in self.opts.Env},
text=True,
encoding="utf-8",
)
GPTScript.__server_url = GPTScript.__process.stderr.readline().strip("\n")
if "=" in GPTScript.__server_url:
GPTScript.__server_url = GPTScript.__server_url.split("=")[1]
self._server_url = f"http://{GPTScript.__server_url}"
self._wait_for_gptscript()
def _wait_for_gptscript(self):
if not GPTScript.__server_ready:
for _ in range(0, 20):
try:
resp = requests.get(self._server_url + "/healthz")
if resp.status_code == 200:
GPTScript.__server_ready = True
return
except requests.exceptions.ConnectionError:
pass
sleep(1)
raise Exception("Failed to start gptscript")
def close(self):
GPTScript.__gptscript_count -= 1
if GPTScript.__gptscript_count == 0 and GPTScript.__process is not None:
GPTScript.__process.stdin.close()
GPTScript.__process.wait()
GPTScript.__server_ready = False
GPTScript.__process = None
self._server_url = ""
def evaluate(
self,
tool: ToolDef | list[ToolDef],
opts: Options = None,
event_handlers: list[Callable[[Run, CallFrame | RunFrame | PromptFrame], Awaitable[None]]] = None
) -> Run:
opts = opts if opts is not None else Options()
return Run(
"evaluate",
tool,
opts.merge_global_opts(self.opts),
self._server_url,
event_handlers=event_handlers,
).next_chat(opts.input)
def run(
self, tool_path: str,
opts: Options = None,
event_handlers: list[Callable[[Run, CallFrame | RunFrame | PromptFrame], Awaitable[None]]] = None
) -> Run:
opts = opts if opts is not None else Options()
return Run(
"run",
tool_path,
opts.merge_global_opts(self.opts),
self._server_url,
event_handlers=event_handlers,
).next_chat(opts.input)
async def load_file(self, file_path: str, disable_cache: bool = False, sub_tool: str = '') -> Program:
out = await self._run_basic_command(
"load",
{"file": file_path, "disableCache": disable_cache, "subTool": sub_tool},
)
parsed_nodes = json.loads(out)
return Program(**parsed_nodes.get("program", {}))
async def load_content(self, content: str, disable_cache: bool = False, sub_tool: str = '') -> Program:
out = await self._run_basic_command(
"load",
{"content": content, "disableCache": disable_cache, "subTool": sub_tool},
)
parsed_nodes = json.loads(out)
return Program(**parsed_nodes.get("program", {}))
async def load_tools(self, tool_defs: list[ToolDef], disable_cache: bool = False, sub_tool: str = '') -> Program:
out = await self._run_basic_command(
"load",
{"toolDefs": [t.to_json() for t in tool_defs], "disableCache": disable_cache, "subTool": sub_tool},
)
parsed_nodes = json.loads(out)
return Program(**parsed_nodes.get("program", {}))
async def parse(self, file_path: str, disable_cache: bool = False) -> list[Text | Tool]:
out = await self._run_basic_command("parse", {"file": file_path, "disableCache": disable_cache})
parsed_nodes = json.loads(out)
if parsed_nodes is None or parsed_nodes.get("nodes", None) is None:
return []
return [Text(**node["textNode"]) if "textNode" in node else Tool(**node.get("toolNode", {}).get("tool", {})) for
node in parsed_nodes.get("nodes", [])]
async def parse_content(self, content: str) -> list[Text | Tool]:
out = await self._run_basic_command("parse", {"content": content})
parsed_nodes = json.loads(out)
if parsed_nodes is None or parsed_nodes.get("nodes", None) is None:
return []
return [Text(**node["textNode"]) if "textNode" in node else Tool(**node.get("toolNode", {}).get("tool", {})) for
node in parsed_nodes.get("nodes", [])]
async def fmt(self, nodes: list[Text | Tool]) -> str:
request_nodes = []
for node in nodes:
request_nodes.append(node.to_json())
return await self._run_basic_command("fmt", {"nodes": request_nodes})
async def confirm(self, resp: AuthResponse):
await self._run_basic_command("confirm/" + resp.id, {**vars(resp)})
async def prompt(self, resp: PromptResponse):
await self._run_basic_command("prompt-response/" + resp.id, resp.responses)
async def _run_basic_command(self, sub_command: str, request_body: Any = None):
run = RunBasicCommand(sub_command, request_body, self._server_url)
run.next_chat()
out = await run.text()
if run.err() != "":
return f"an error occurred: {out}"
return out
async def version(self) -> str:
return await self._run_basic_command("version")
async def list_models(self, providers: list[str] = None, credential_overrides: list[str] = None) -> list[str]:
if self.opts.DefaultModelProvider != "":
if providers is None:
providers = []
providers.append(self.opts.DefaultModelProvider)
return (await self._run_basic_command(
"list-models",
{"providers": providers, "credentialOverrides": credential_overrides}
)).split("\n")
async def list_credentials(self, contexts: List[str] = None, all_contexts: bool = False) -> list[Credential] | str:
if contexts is None:
contexts = ["default"]
res = await self._run_basic_command(
"credentials",
{"context": contexts, "allContexts": all_contexts}
)
if res.startswith("an error occurred:"):
return res
return [to_credential(cred) for cred in json.loads(res)]
async def create_credential(self, cred: Credential) -> str:
return await self._run_basic_command(
"credentials/create",
{"content": cred.to_json()}
)
async def reveal_credential(self, contexts: List[str] = None, name: str = "") -> Credential | str:
if contexts is None:
contexts = ["default"]
res = await self._run_basic_command(
"credentials/reveal",
{"context": contexts, "name": name}
)
if res.startswith("an error occurred:"):
return res
return to_credential(json.loads(res))
async def delete_credential(self, context: str = "default", name: str = "") -> str:
return await self._run_basic_command(
"credentials/delete",
{"context": [context], "name": name}
)
def _get_command():
if os.getenv("GPTSCRIPT_BIN") is not None:
return os.getenv("GPTSCRIPT_BIN")
bin_path = os.path.join(os.path.dirname(executable), "gptscript")
if platform.system() == "Windows":
bin_path += ".exe"
return bin_path if os.path.exists(bin_path) else "gptscript"