freeplay 0.2.27__tar.gz → 0.2.30__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- freeplay-0.2.30/LICENSE +21 -0
- {freeplay-0.2.27 → freeplay-0.2.30}/PKG-INFO +2 -2
- {freeplay-0.2.27 → freeplay-0.2.30}/pyproject.toml +10 -2
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/completions.py +10 -5
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/flavors.py +107 -77
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/freeplay.py +9 -2
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/provider_config.py +1 -1
- {freeplay-0.2.27 → freeplay-0.2.30}/README.md +0 -0
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/__init__.py +0 -0
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/api_support.py +0 -0
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/errors.py +0 -0
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/freeplay_cli.py +0 -0
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/freeplay_thin.py +0 -0
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/llm_parameters.py +0 -0
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/record.py +0 -0
- {freeplay-0.2.27 → freeplay-0.2.30/src}/freeplay/utils.py +0 -0
freeplay-0.2.30/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 228Labs
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: freeplay
|
3
|
-
Version: 0.2.
|
3
|
+
Version: 0.2.30
|
4
4
|
Summary:
|
5
5
|
License: MIT
|
6
6
|
Author: FreePlay Engineering
|
@@ -15,7 +15,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
15
15
|
Requires-Dist: anthropic (>=0.7.7,<0.8.0)
|
16
16
|
Requires-Dist: click (==8.1.7)
|
17
17
|
Requires-Dist: dacite (>=1.8.0,<2.0.0)
|
18
|
-
Requires-Dist: openai (>=
|
18
|
+
Requires-Dist: openai (>=1,<2)
|
19
19
|
Requires-Dist: pystache (>=0.6.5,<0.7.0)
|
20
20
|
Requires-Dist: requests (>=2.20.0,<3.0.0dev)
|
21
21
|
Description-Content-Type: text/markdown
|
@@ -1,6 +1,6 @@
|
|
1
1
|
[tool.poetry]
|
2
2
|
name = "freeplay"
|
3
|
-
version = "0.2.
|
3
|
+
version = "0.2.30"
|
4
4
|
description = ""
|
5
5
|
authors = ["FreePlay Engineering <engineering@freeplay.ai>"]
|
6
6
|
license = "MIT"
|
@@ -11,10 +11,18 @@ python = ">=3.8, <4"
|
|
11
11
|
requests = ">=2.20.0,<3.0.0dev"
|
12
12
|
dacite = "^1.8.0"
|
13
13
|
anthropic = "^0.7.7"
|
14
|
-
openai = "^
|
14
|
+
openai = "^1"
|
15
15
|
click = "8.1.7"
|
16
16
|
pystache = "^0.6.5"
|
17
17
|
|
18
|
+
[tool.poetry.group.dev.dependencies]
|
19
|
+
mypy = "^1"
|
20
|
+
types-requests = "^2.31"
|
21
|
+
|
22
|
+
[tool.poetry.group.test.dependencies]
|
23
|
+
responses = "^0.23.1"
|
24
|
+
respx = "^0.20.2"
|
25
|
+
|
18
26
|
[build-system]
|
19
27
|
requires = ["poetry-core"]
|
20
28
|
build-backend = "poetry.core.masonry.api"
|
@@ -1,17 +1,22 @@
|
|
1
1
|
from dataclasses import dataclass
|
2
|
-
from typing import Any, Dict, List, Optional
|
2
|
+
from typing import Any, Dict, List, Optional, TypedDict
|
3
|
+
|
4
|
+
from openai.types.chat.chat_completion_chunk import ChoiceDeltaFunctionCall
|
5
|
+
from openai.types.chat.chat_completion_message import FunctionCall
|
3
6
|
|
4
7
|
from .llm_parameters import LLMParameters
|
5
8
|
|
6
|
-
|
7
|
-
|
9
|
+
|
10
|
+
class ChatMessage(TypedDict):
|
11
|
+
role: str
|
12
|
+
content: str
|
8
13
|
|
9
14
|
|
10
15
|
@dataclass
|
11
16
|
class CompletionResponse:
|
12
17
|
content: str
|
13
18
|
is_complete: bool
|
14
|
-
openai_function_call: Optional[
|
19
|
+
openai_function_call: Optional[FunctionCall] = None
|
15
20
|
|
16
21
|
|
17
22
|
@dataclass
|
@@ -44,4 +49,4 @@ class PromptTemplates:
|
|
44
49
|
class CompletionChunk:
|
45
50
|
text: str
|
46
51
|
is_complete: bool
|
47
|
-
openai_function_call: Optional[
|
52
|
+
openai_function_call: Optional[ChoiceDeltaFunctionCall] = None
|
@@ -1,17 +1,18 @@
|
|
1
1
|
import json
|
2
2
|
from abc import abstractmethod, ABC
|
3
3
|
from copy import copy
|
4
|
-
from typing import Any, Dict, Generator, List, Optional
|
4
|
+
from typing import cast, Any, Dict, Generator, List, Optional, Union
|
5
5
|
|
6
|
-
import anthropic
|
6
|
+
import anthropic
|
7
7
|
import openai
|
8
|
-
from openai
|
8
|
+
from openai import AuthenticationError, BadRequestError, Stream
|
9
|
+
from openai.types.chat import ChatCompletion, ChatCompletionChunk, ChatCompletionMessageParam
|
9
10
|
|
10
11
|
from .completions import CompletionChunk, PromptTemplateWithMetadata, CompletionResponse, ChatCompletionResponse, \
|
11
12
|
ChatMessage
|
12
13
|
from .errors import FreeplayConfigurationError, LLMClientError, LLMServerError, FreeplayError
|
13
14
|
from .llm_parameters import LLMParameters
|
14
|
-
from .provider_config import
|
15
|
+
from .provider_config import AnthropicConfig, AzureConfig, OpenAIConfig, ProviderConfig
|
15
16
|
from .utils import format_template_variables
|
16
17
|
|
17
18
|
|
@@ -89,45 +90,17 @@ class ChatFlavor(Flavor, ABC):
|
|
89
90
|
pass
|
90
91
|
|
91
92
|
|
92
|
-
class
|
93
|
-
def configure_openai(
|
94
|
-
self,
|
95
|
-
openai_config: Optional[OpenAIConfig],
|
96
|
-
api_base: Optional[str] = None,
|
97
|
-
api_version: Optional[str] = None,
|
98
|
-
api_type: Optional[str] = None,
|
99
|
-
) -> None:
|
100
|
-
super().__init__()
|
101
|
-
if not openai_config:
|
102
|
-
raise FreeplayConfigurationError(
|
103
|
-
"Missing OpenAI key. Use a ProviderConfig to specify keys prior to getting completion.")
|
104
|
-
|
105
|
-
if api_base:
|
106
|
-
openai.api_base = api_base
|
107
|
-
elif openai_config.api_base:
|
108
|
-
openai.api_base = openai_config.api_base
|
109
|
-
|
110
|
-
if api_type:
|
111
|
-
openai.api_type = api_type
|
112
|
-
|
113
|
-
if api_version:
|
114
|
-
openai.api_version = api_version
|
115
|
-
|
116
|
-
if not openai_config.api_key or not openai_config.api_key.strip():
|
117
|
-
raise FreeplayConfigurationError("OpenAI API key is not set. It must be set to make calls to the service.")
|
118
|
-
|
119
|
-
openai.api_key = openai_config.api_key
|
93
|
+
class OpenAIChatFlavor(ChatFlavor, ABC):
|
120
94
|
|
121
|
-
@
|
122
|
-
def
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
})
|
95
|
+
@abstractmethod
|
96
|
+
def _call_openai(
|
97
|
+
self,
|
98
|
+
messages: List[ChatMessage],
|
99
|
+
provider_config: ProviderConfig,
|
100
|
+
llm_parameters: LLMParameters,
|
101
|
+
stream: bool
|
102
|
+
) -> Union[ChatCompletion, openai.Stream[ChatCompletionChunk]]:
|
103
|
+
pass
|
131
104
|
|
132
105
|
def format(self, prompt_template: PromptTemplateWithMetadata, variables: Dict[str, str]) -> str:
|
133
106
|
# Extract messages JSON to enable formatting of individual content fields of each message. If we do not
|
@@ -146,11 +119,12 @@ class OpenAIChat(OpenAI, ChatFlavor):
|
|
146
119
|
llm_parameters: LLMParameters
|
147
120
|
) -> CompletionResponse:
|
148
121
|
messages = json.loads(formatted_prompt)
|
149
|
-
completion = self._call_openai(messages, provider_config, llm_parameters, stream=False)
|
122
|
+
completion = cast(ChatCompletion, self._call_openai(messages, provider_config, llm_parameters, stream=False))
|
123
|
+
|
150
124
|
return CompletionResponse(
|
151
125
|
content=completion.choices[0].message.content or '',
|
152
126
|
is_complete=completion.choices[0].finish_reason == 'stop',
|
153
|
-
openai_function_call=completion.choices[0].message.
|
127
|
+
openai_function_call=completion.choices[0].message.function_call,
|
154
128
|
)
|
155
129
|
|
156
130
|
def call_service_stream(
|
@@ -160,12 +134,13 @@ class OpenAIChat(OpenAI, ChatFlavor):
|
|
160
134
|
llm_parameters: LLMParameters
|
161
135
|
) -> Generator[CompletionChunk, None, None]:
|
162
136
|
messages = json.loads(formatted_prompt)
|
163
|
-
completion_stream =
|
137
|
+
completion_stream = cast(Stream[ChatCompletionChunk],
|
138
|
+
self._call_openai(messages, provider_config, llm_parameters, stream=True))
|
164
139
|
for chunk in completion_stream:
|
165
140
|
yield CompletionChunk(
|
166
|
-
text=chunk.choices[0].delta.
|
141
|
+
text=chunk.choices[0].delta.content or '',
|
167
142
|
is_complete=chunk.choices[0].finish_reason == 'stop',
|
168
|
-
openai_function_call=chunk.choices[0].delta.
|
143
|
+
openai_function_call=chunk.choices[0].delta.function_call
|
169
144
|
)
|
170
145
|
|
171
146
|
def continue_chat(
|
@@ -174,12 +149,16 @@ class OpenAIChat(OpenAI, ChatFlavor):
|
|
174
149
|
provider_config: ProviderConfig,
|
175
150
|
llm_parameters: LLMParameters
|
176
151
|
) -> ChatCompletionResponse:
|
177
|
-
completion = self._call_openai(messages, provider_config, llm_parameters, stream=False)
|
152
|
+
completion = cast(ChatCompletion, self._call_openai(messages, provider_config, llm_parameters, stream=False))
|
178
153
|
|
179
154
|
message_history = copy(messages)
|
180
|
-
|
155
|
+
message = completion.choices[0].message
|
156
|
+
message_history.append({
|
157
|
+
"role": message.role or '',
|
158
|
+
"content": message.content or ''
|
159
|
+
})
|
181
160
|
return ChatCompletionResponse(
|
182
|
-
content=
|
161
|
+
content=message.content or '',
|
183
162
|
message_history=message_history,
|
184
163
|
is_complete=completion.choices[0].finish_reason == "stop"
|
185
164
|
)
|
@@ -190,37 +169,91 @@ class OpenAIChat(OpenAI, ChatFlavor):
|
|
190
169
|
provider_config: ProviderConfig,
|
191
170
|
llm_parameters: LLMParameters
|
192
171
|
) -> Generator[CompletionChunk, None, None]:
|
193
|
-
completion_stream =
|
172
|
+
completion_stream = cast(Stream[ChatCompletionChunk],
|
173
|
+
self._call_openai(messages, provider_config, llm_parameters, stream=True))
|
194
174
|
for chunk in completion_stream:
|
195
175
|
yield CompletionChunk(
|
196
|
-
text=chunk.choices[0].delta.
|
176
|
+
text=chunk.choices[0].delta.content or '',
|
197
177
|
is_complete=chunk.choices[0].finish_reason == "stop"
|
198
178
|
)
|
199
179
|
|
180
|
+
|
181
|
+
class OpenAIChat(OpenAIChatFlavor):
|
182
|
+
record_format_type = "openai_chat"
|
183
|
+
_model_params_with_defaults = LLMParameters({
|
184
|
+
"model": "gpt-3.5-turbo"
|
185
|
+
})
|
186
|
+
|
187
|
+
def __init__(self) -> None:
|
188
|
+
self.client: Optional[openai.OpenAI] = None
|
189
|
+
|
190
|
+
@property
|
191
|
+
def provider(self) -> str:
|
192
|
+
return "openai"
|
193
|
+
|
194
|
+
def get_openai_client(self, openai_config: Optional[OpenAIConfig]) -> openai.OpenAI:
|
195
|
+
if self.client:
|
196
|
+
return self.client
|
197
|
+
|
198
|
+
if not openai_config:
|
199
|
+
raise FreeplayConfigurationError(
|
200
|
+
"Missing OpenAI key. Use a ProviderConfig to specify keys prior to getting completion.")
|
201
|
+
|
202
|
+
self.client = openai.OpenAI(api_key=openai_config.api_key, base_url=openai_config.base_url)
|
203
|
+
return self.client
|
204
|
+
|
200
205
|
def _call_openai(
|
201
206
|
self,
|
202
207
|
messages: List[ChatMessage],
|
203
208
|
provider_config: ProviderConfig,
|
204
209
|
llm_parameters: LLMParameters,
|
205
210
|
stream: bool
|
206
|
-
) ->
|
207
|
-
self.
|
208
|
-
llm_parameters.pop('endpoint')
|
211
|
+
) -> Union[ChatCompletion, openai.Stream[ChatCompletionChunk]]:
|
212
|
+
client = self.get_openai_client(provider_config.openai)
|
209
213
|
try:
|
210
|
-
return
|
211
|
-
messages=messages,
|
214
|
+
return client.chat.completions.create(
|
215
|
+
messages=cast(List[ChatCompletionMessageParam], messages),
|
212
216
|
**self.get_model_params(llm_parameters),
|
213
217
|
stream=stream,
|
214
|
-
)
|
215
|
-
except (
|
218
|
+
)
|
219
|
+
except (BadRequestError, AuthenticationError) as e:
|
216
220
|
raise LLMClientError("Unable to call OpenAI") from e
|
217
221
|
except Exception as e:
|
218
222
|
raise LLMServerError("Unable to call OpenAI") from e
|
219
223
|
|
220
224
|
|
221
|
-
class AzureOpenAIChat(
|
225
|
+
class AzureOpenAIChat(OpenAIChatFlavor):
|
222
226
|
record_format_type = "azure_openai_chat"
|
223
227
|
|
228
|
+
def __init__(self) -> None:
|
229
|
+
self.client: Optional[openai.AzureOpenAI] = None
|
230
|
+
|
231
|
+
@property
|
232
|
+
def provider(self) -> str:
|
233
|
+
return "azure"
|
234
|
+
|
235
|
+
def get_azure_client(
|
236
|
+
self,
|
237
|
+
azure_config: Optional[AzureConfig],
|
238
|
+
api_version: Optional[str] = None,
|
239
|
+
endpoint: Optional[str] = None,
|
240
|
+
deployment: Optional[str] = None,
|
241
|
+
) -> openai.AzureOpenAI:
|
242
|
+
if self.client:
|
243
|
+
return self.client
|
244
|
+
|
245
|
+
if not azure_config:
|
246
|
+
raise FreeplayConfigurationError(
|
247
|
+
"Missing Azure key. Use a ProviderConfig to specify keys prior to getting completion.")
|
248
|
+
|
249
|
+
self.client = openai.AzureOpenAI(
|
250
|
+
api_key=azure_config.api_key,
|
251
|
+
api_version=api_version,
|
252
|
+
azure_endpoint=endpoint or '',
|
253
|
+
azure_deployment=deployment,
|
254
|
+
)
|
255
|
+
return self.client
|
256
|
+
|
224
257
|
def _call_openai(
|
225
258
|
self,
|
226
259
|
messages: List[ChatMessage],
|
@@ -232,28 +265,25 @@ class AzureOpenAIChat(OpenAIChat):
|
|
232
265
|
deployment_id = llm_parameters.get('deployment_id')
|
233
266
|
resource_name = llm_parameters.get('resource_name')
|
234
267
|
endpoint = f'https://{resource_name}.openai.azure.com'
|
235
|
-
self.configure_openai(
|
236
|
-
provider_config.azure,
|
237
|
-
api_base=endpoint,
|
238
|
-
api_type='azure',
|
239
|
-
api_version=api_version
|
240
|
-
)
|
241
268
|
llm_parameters.pop('resource_name')
|
269
|
+
|
270
|
+
client = self.get_azure_client(
|
271
|
+
azure_config=provider_config.azure,
|
272
|
+
api_version=api_version,
|
273
|
+
endpoint=endpoint,
|
274
|
+
deployment=deployment_id,
|
275
|
+
)
|
276
|
+
|
242
277
|
try:
|
243
|
-
return
|
244
|
-
messages=messages,
|
278
|
+
return client.chat.completions.create(
|
279
|
+
messages=cast(List[ChatCompletionMessageParam], messages),
|
245
280
|
**self.get_model_params(llm_parameters),
|
246
|
-
engine=deployment_id,
|
247
281
|
stream=stream,
|
248
|
-
)
|
249
|
-
except (
|
250
|
-
raise LLMClientError("Unable to call
|
282
|
+
)
|
283
|
+
except (BadRequestError, AuthenticationError) as e:
|
284
|
+
raise LLMClientError("Unable to call Azure") from e
|
251
285
|
except Exception as e:
|
252
|
-
raise LLMServerError("Unable to call
|
253
|
-
|
254
|
-
@property
|
255
|
-
def provider(self) -> str:
|
256
|
-
return "azure"
|
286
|
+
raise LLMServerError("Unable to call Azure") from e
|
257
287
|
|
258
288
|
|
259
289
|
class AnthropicClaudeText(Flavor):
|
@@ -7,8 +7,14 @@ from typing import cast, Any, Dict, Generator, List, Optional, Tuple, Union
|
|
7
7
|
|
8
8
|
from . import api_support
|
9
9
|
from .api_support import try_decode
|
10
|
-
from .completions import
|
11
|
-
|
10
|
+
from .completions import (
|
11
|
+
PromptTemplates,
|
12
|
+
CompletionResponse,
|
13
|
+
CompletionChunk,
|
14
|
+
PromptTemplateWithMetadata,
|
15
|
+
ChatCompletionResponse,
|
16
|
+
ChatMessage
|
17
|
+
)
|
12
18
|
from .errors import FreeplayConfigurationError, freeplay_response_error, FreeplayServerError
|
13
19
|
from .flavors import Flavor, ChatFlavor
|
14
20
|
from .llm_parameters import LLMParameters
|
@@ -700,6 +706,7 @@ def require_chat_flavor(flavor: Flavor) -> ChatFlavor:
|
|
700
706
|
|
701
707
|
return flavor
|
702
708
|
|
709
|
+
|
703
710
|
def check_all_values_string_or_number(metadata: Optional[Dict[str, Union[str,int,float]]]) -> None:
|
704
711
|
if metadata:
|
705
712
|
for key, value in metadata.items():
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|