-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnlp.py
256 lines (215 loc) · 7.85 KB
/
nlp.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""A module for running OpenAI functions"""
from __future__ import annotations
from dataclasses import dataclass
from functools import partial
from typing import Callable, Generic, Protocol, TypeVar, overload
from typing_extensions import ParamSpec
from .conversation import Conversation
from .functions.wrapper import FunctionWrapper, WrapperConfig
Param = ParamSpec("Param")
Return = TypeVar("Return")
T = TypeVar("T")
@dataclass
class NaturalLanguageAnnotated(Generic[T]):
"""A natural language annotated function return value"""
function_result: T
annotation: str
# This is a callable protocol, thus pylint can shut up
class DecoratorProtocol(
Protocol[Param, Return]
): # pylint: disable=too-few-public-methods
"""A protocol for the nlp decorator"""
def __call__(
self,
function: Callable[Param, Return],
*,
system_prompt: str | None = None,
model: str = "gpt-3.5-turbo-0613",
engine: str | None = None,
) -> Wrapper[Param, Return]:
...
@dataclass
class NLPWrapperConfig:
"""A configuration for the nlp decorator"""
name: str | None = None
description: str | None = None
serialize: bool = True
model: str = "gpt-3.5-turbo-0613"
engine: str | None = None
system_prompt: str | None = None
class Wrapper(Generic[Param, Return]):
"""A wrapper for a function that provides a natural language interface"""
def __init__(
self,
origin: Callable[..., Return],
config: NLPWrapperConfig,
) -> None:
self.origin = origin
self.config = config
self.conversation = Conversation(model=config.model, engine=config.engine)
self.openai_function = FunctionWrapper(
self.origin,
WrapperConfig(serialize=config.serialize),
name=config.name,
description=config.description,
)
self.conversation.add_function(self.openai_function)
def __call__(self, *args: Param.args, **kwds: Param.kwargs) -> Return:
return self.origin(*args, **kwds)
def _initialize_conversation(self) -> None:
"""Initialize the conversation"""
self.conversation.clear_messages()
if self.config.system_prompt is not None:
self.conversation.add_message(
{
"role": "system",
"content": self.config.system_prompt,
}
)
def from_natural_language(self, prompt: str, retries: int | None = 1) -> Return:
"""Run the function with the given natural language input
Args:
prompt (str): The prompt to use
retries (int | None): The number of retries; if None, will retry
indefinitely
Returns:
The result of the original function
"""
self._initialize_conversation()
return self.conversation.run(self.openai_function.name, prompt, retries=retries)
def natural_language_response(self, prompt: str, retries: int | None = 1) -> str:
"""Run the function and respond to the user with natural language
Args:
prompt (str): The prompt to use
retries (int | None): The number of retries; if None, will retry
indefinitely
Returns:
str: The response from the AI
"""
self._initialize_conversation()
self.conversation.add_message(prompt)
self.conversation.generate_message(
function_call={"name": self.openai_function.name}
)
response = self.conversation.run_until_response(False, retries=retries)
return response.content
def natural_language_annotated(
self, prompt: str, retries: int | None = 1
) -> NaturalLanguageAnnotated[Return]:
"""Run the function and respond to the user with natural language as well as
the raw function result
Args:
prompt (str): The prompt to use
retries (int | None): The number of retries; if None, will retry
indefinitely
Returns:
NaturalLanguageAnnotated: The response from the AI
"""
self._initialize_conversation()
function_result = self.conversation.run(
self.openai_function.name, prompt, retries=retries
)
response = self.conversation.run_until_response(False, retries=retries)
return NaturalLanguageAnnotated(function_result, response.content)
def _nlp(
function: Callable[Param, Return],
*,
name: str | None = None,
description: str | None = None,
system_prompt: str | None = None,
model: str = "gpt-3.5-turbo-0613",
engine: str | None = None,
serialize: bool = True,
) -> Wrapper[Param, Return]:
"""Add natural language input to a function
Args:
function (Callable): The function to add natural language input to
system_prompt (str | None): The system prompt to use. Defaults to None.
model (str): The model to use. Defaults to "gpt-3.5-turbo-0613".
engine (str | None): The engine to use, for example, for Azure deployments.
name (str | None): The name override for the function.
description (str | None): The description sent to OpenAI.
serialize (bool): Whether to serialize the function result.
Returns:
The function, with natural language input, or a decorator to add natural
language input to a function
"""
return Wrapper(
function,
NLPWrapperConfig(
system_prompt=system_prompt,
model=model,
engine=engine,
name=name,
description=description,
serialize=serialize,
),
)
@overload
def nlp(
function: Callable[Param, Return],
*,
name: str | None = None,
description: str | None = None,
serialize: bool = True,
system_prompt: str | None = None,
model: str = "gpt-3.5-turbo-0613",
engine: str | None = None,
) -> Wrapper[Param, Return]:
...
@overload
def nlp(
*,
name: str | None = None,
description: str | None = None,
serialize: bool = True,
system_prompt: str | None = None,
model: str = "gpt-3.5-turbo-0613",
engine: str | None = None,
) -> DecoratorProtocol:
...
def nlp(
function: Callable[Param, Return] | None = None,
*,
name: str | None = None,
description: str | None = None,
serialize: bool = True,
system_prompt: str | None = None,
model: str = "gpt-3.5-turbo-0613",
engine: str | None = None,
) -> Wrapper[Param, Return] | DecoratorProtocol:
"""Add natural language input to a function
Args:
function (Callable | None): The function
to add natural language input to
name (str | None): The name override for the function, will be inferred from
the function name if not provided.
description (str | None): The description sent to OpenAI, defaults to the short
description from the function docstring.
serialize (bool): Whether to serialize the function result.
system_prompt (str | None): The system prompt to use. Defaults to None.
model (str): The model to use. Defaults to "gpt-3.5-turbo-0613".
engine (str | None): The engine to use, for example, for Azure deployments.
Returns:
Wrapper | DecoratorProtocol: The function, with natural language input, or a
decorator to add natural language input to a function
"""
if function is None:
return partial(
_nlp,
name=name,
description=description,
serialize=serialize,
system_prompt=system_prompt,
model=model,
engine=engine,
)
return _nlp(
function,
name=name,
description=description,
serialize=serialize,
system_prompt=system_prompt,
model=model,
engine=engine,
)