gnarly 0.0.0.dev9__tar.gz → 0.0.0.dev10__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.
Potentially problematic release.
This version of gnarly might be problematic. Click here for more details.
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/PKG-INFO +1 -1
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/source/conf.py +2 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/pyproject.toml +1 -1
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/utils/llm/interfaces.py +202 -57
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/utils/llm/models.py +121 -28
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/uv.lock +1 -1
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/.gitignore +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/.python-version +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/README.md +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/Makefile +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/api/gnarly.rst +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/api/gnarly.utils.code.rst +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/api/gnarly.utils.llm.rst +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/api/gnarly.utils.rst +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/api/modules.rst +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/make.bat +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/source/api/gnarly.rst +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/source/api/gnarly.utils.code.rst +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/source/api/gnarly.utils.llm.rst +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/source/api/gnarly.utils.rst +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/source/api/modules.rst +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/source/api.md +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/source/index.md +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/docs/source/usage.md +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/__init__.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/constants.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/errors.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/types.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/utils/__init__.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/utils/code/__init__.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/utils/code/projects.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/utils/io.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/utils/llm/__init__.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/utils/llm/chatgpt.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/src/gnarly/utils/llm/claude.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/tests/__init__.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/tests/test_constants.py +0 -0
- {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev10}/tests/test_io_utilities.py +0 -0
|
@@ -51,13 +51,53 @@ except IOError as e:
|
|
|
51
51
|
|
|
52
52
|
# ─── interfaces ──────────────────────────────────────────────────────── ✦✦ ──
|
|
53
53
|
class Claude(ClaudeModel):
|
|
54
|
+
"""
|
|
55
|
+
Interface for interacting with Claude AI via the Anthropic API.
|
|
56
|
+
|
|
57
|
+
This class provides a conversational interface to Claude, maintaining
|
|
58
|
+
conversation history and handling API communication.
|
|
59
|
+
|
|
60
|
+
Parameters
|
|
61
|
+
----------
|
|
62
|
+
system_prompt : str, optional
|
|
63
|
+
The system prompt to use for conversations, by default
|
|
64
|
+
"You are Claude, a friendly AI assistant."
|
|
65
|
+
|
|
66
|
+
Attributes
|
|
67
|
+
----------
|
|
68
|
+
api_key : str
|
|
69
|
+
The Anthropic API key retrieved from environment variables.
|
|
70
|
+
conversation : List[Dict]
|
|
71
|
+
List of conversation messages with 'role' and 'content' keys.
|
|
72
|
+
max_tokens : int
|
|
73
|
+
Maximum number of tokens for responses, by default 4096.
|
|
74
|
+
system_prompt : str
|
|
75
|
+
The system prompt used for conversations.
|
|
76
|
+
temperature : float
|
|
77
|
+
Sampling temperature for response generation, by default 0.2.
|
|
78
|
+
save_to : Path
|
|
79
|
+
Directory path where conversations are saved.
|
|
80
|
+
|
|
81
|
+
Raises
|
|
82
|
+
------
|
|
83
|
+
ValueError
|
|
84
|
+
If ANTHROPIC_API_KEY environment variable is not set.
|
|
85
|
+
|
|
86
|
+
Examples
|
|
87
|
+
--------
|
|
88
|
+
>>> claude = Claude()
|
|
89
|
+
>>> response, full_result = claude.message("Hello!")
|
|
90
|
+
>>> print(response['content'])
|
|
91
|
+
Hello! How can I help you today?
|
|
92
|
+
"""
|
|
93
|
+
|
|
54
94
|
def __init__(
|
|
55
95
|
self,
|
|
56
96
|
system_prompt: str = "You are Claude, a friendly AI assistant."
|
|
57
97
|
):
|
|
58
98
|
super().__init__()
|
|
59
|
-
self.
|
|
60
|
-
if not self.
|
|
99
|
+
self._api_key = self.api_key or os.getenv("ANTHROPIC_API_KEY", "")
|
|
100
|
+
if not self._api_key:
|
|
61
101
|
raise ValueError("ANTHROPIC_API_KEY environment variable is required")
|
|
62
102
|
|
|
63
103
|
self.conversation: List[Dict] = []
|
|
@@ -71,11 +111,48 @@ class Claude(ClaudeModel):
|
|
|
71
111
|
# Create directory if it doesn't exist
|
|
72
112
|
self.save_to.parent.mkdir(parents=True, exist_ok=True)
|
|
73
113
|
logger.info(f"Conversation will be saved to {self.save_to}")
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def api_key(self) -> str:
|
|
117
|
+
"""
|
|
118
|
+
Get the API key (masked for security).
|
|
119
|
+
|
|
120
|
+
Returns
|
|
121
|
+
-------
|
|
122
|
+
str
|
|
123
|
+
Masked API key string.
|
|
124
|
+
"""
|
|
125
|
+
if hasattr(self, '_api_key') and self._api_key:
|
|
126
|
+
return f"sk-...{self._api_key[-4:]}" if len(self._api_key) > 4 else "***"
|
|
127
|
+
return ""
|
|
74
128
|
|
|
75
129
|
def message(self, msg: str) -> Tuple[Dict, Dict]:
|
|
76
130
|
"""
|
|
77
|
-
|
|
78
|
-
|
|
131
|
+
Send a message to Claude and get a response.
|
|
132
|
+
|
|
133
|
+
Parameters
|
|
134
|
+
----------
|
|
135
|
+
msg : str
|
|
136
|
+
The message to send to Claude.
|
|
137
|
+
|
|
138
|
+
Returns
|
|
139
|
+
-------
|
|
140
|
+
Tuple[Dict, Dict]
|
|
141
|
+
A tuple containing:
|
|
142
|
+
- response: Dict with 'role' and 'content' keys
|
|
143
|
+
- full_result: Complete API response dictionary
|
|
144
|
+
|
|
145
|
+
Raises
|
|
146
|
+
------
|
|
147
|
+
Exception
|
|
148
|
+
If the API request fails or returns an error.
|
|
149
|
+
|
|
150
|
+
Examples
|
|
151
|
+
--------
|
|
152
|
+
>>> claude = Claude()
|
|
153
|
+
>>> response, result = claude.message("What is 2+2?")
|
|
154
|
+
>>> print(response['content'])
|
|
155
|
+
2 + 2 = 4
|
|
79
156
|
"""
|
|
80
157
|
self.conversation.append({"role": "user", "content": msg})
|
|
81
158
|
|
|
@@ -91,7 +168,7 @@ class Claude(ClaudeModel):
|
|
|
91
168
|
}
|
|
92
169
|
|
|
93
170
|
headers = {
|
|
94
|
-
"x-api-key": self.
|
|
171
|
+
"x-api-key": self._api_key,
|
|
95
172
|
"Content-Type": "application/json",
|
|
96
173
|
"anthropic-version": "2023-06-01"
|
|
97
174
|
}
|
|
@@ -123,14 +200,27 @@ class Claude(ClaudeModel):
|
|
|
123
200
|
|
|
124
201
|
def save(self, save_as: Union[str, Path] = None) -> int:
|
|
125
202
|
"""
|
|
126
|
-
Save the current conversation to a file.
|
|
203
|
+
Save the current conversation to a JSON file.
|
|
127
204
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
205
|
+
Parameters
|
|
206
|
+
----------
|
|
207
|
+
save_as : Union[str, Path], optional
|
|
208
|
+
Path to save the conversation. If not provided, uses the default
|
|
209
|
+
path from initialization. Relative paths are resolved relative
|
|
210
|
+
to the Claude conversations directory.
|
|
211
|
+
|
|
212
|
+
Returns
|
|
213
|
+
-------
|
|
214
|
+
int
|
|
215
|
+
0 on success, 1 on failure.
|
|
216
|
+
|
|
217
|
+
Examples
|
|
218
|
+
--------
|
|
219
|
+
>>> claude = Claude()
|
|
220
|
+
>>> claude.message("Hello")
|
|
221
|
+
>>> result = claude.save("my_conversation.json")
|
|
222
|
+
>>> print(result)
|
|
223
|
+
0
|
|
134
224
|
"""
|
|
135
225
|
try:
|
|
136
226
|
target_path = Path(save_as) if save_as else self.save_to
|
|
@@ -148,29 +238,52 @@ class Claude(ClaudeModel):
|
|
|
148
238
|
logger.error(f"Failed to save conversation: {str(e)}")
|
|
149
239
|
return 1
|
|
150
240
|
|
|
241
|
+
|
|
151
242
|
class ChatGPT(ChatGPTModel):
|
|
152
|
-
"""
|
|
243
|
+
"""
|
|
244
|
+
A turn-based, chat-like interface for the OpenAI API.
|
|
153
245
|
|
|
154
|
-
This class
|
|
246
|
+
This class provides a conversational interface to ChatGPT models,
|
|
247
|
+
maintaining conversation history and handling API communication
|
|
248
|
+
via HTTP requests.
|
|
155
249
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
250
|
+
Parameters
|
|
251
|
+
----------
|
|
252
|
+
model : str, optional
|
|
253
|
+
The ChatGPT model identifier to use, by default "gpt-4".
|
|
254
|
+
instructions : str, optional
|
|
255
|
+
The system prompt/instructions for the model, by default
|
|
256
|
+
"You are ChatGPT, a helpful AI assistant.".
|
|
257
|
+
|
|
258
|
+
Attributes
|
|
259
|
+
----------
|
|
260
|
+
api_key : str
|
|
261
|
+
The OpenAI API key retrieved from environment variables.
|
|
262
|
+
conversation : List[Dict]
|
|
263
|
+
List of message dictionaries representing the conversation history.
|
|
264
|
+
Each message has 'role' and 'content' keys.
|
|
265
|
+
model : str
|
|
266
|
+
The ChatGPT model identifier.
|
|
267
|
+
instructions : str
|
|
268
|
+
The system instructions/prompt.
|
|
269
|
+
max_tokens : int
|
|
270
|
+
Maximum number of tokens for model output, by default 4096.
|
|
271
|
+
save_to : Path
|
|
272
|
+
Directory path where conversations are saved.
|
|
273
|
+
temperature : float
|
|
274
|
+
Sampling temperature for response randomness, by default 0.2.
|
|
275
|
+
|
|
276
|
+
Raises
|
|
277
|
+
------
|
|
278
|
+
ValueError
|
|
279
|
+
If OPENAI_API_KEY environment variable is not set.
|
|
280
|
+
|
|
281
|
+
Examples
|
|
282
|
+
--------
|
|
283
|
+
>>> chatgpt = ChatGPT(model="gpt-4", instructions="You are a helpful assistant.")
|
|
284
|
+
>>> response = chatgpt.message("Hello!")
|
|
285
|
+
>>> print(response)
|
|
286
|
+
Hello! How can I help you today?
|
|
174
287
|
"""
|
|
175
288
|
|
|
176
289
|
def __init__(
|
|
@@ -178,22 +291,9 @@ class ChatGPT(ChatGPTModel):
|
|
|
178
291
|
model: str = "gpt-4",
|
|
179
292
|
instructions: str = "You are ChatGPT, a helpful AI assistant."
|
|
180
293
|
) -> None:
|
|
181
|
-
"""Initialize a `ChatGPT` instance.
|
|
182
|
-
|
|
183
|
-
Args:
|
|
184
|
-
model (str):
|
|
185
|
-
A string representing the ChatGPT model to be called.
|
|
186
|
-
Defaults to "gpt-4".
|
|
187
|
-
instructions (str):
|
|
188
|
-
A string representing the system prompt to be used.
|
|
189
|
-
Defaults to "You are ChatGPT, a helpful AI assistant.".
|
|
190
|
-
|
|
191
|
-
Returns:
|
|
192
|
-
`None`.
|
|
193
|
-
"""
|
|
194
294
|
super().__init__()
|
|
195
|
-
self.
|
|
196
|
-
if not self.
|
|
295
|
+
self._api_key = self.api_key or os.getenv("OPENAI_API_KEY", "")
|
|
296
|
+
if not self._api_key:
|
|
197
297
|
raise ValueError("OPENAI_API_KEY environment variable is required")
|
|
198
298
|
|
|
199
299
|
self.conversation: List[Dict] = []
|
|
@@ -205,17 +305,48 @@ class ChatGPT(ChatGPTModel):
|
|
|
205
305
|
|
|
206
306
|
# Ensure that the conversations directory exists.
|
|
207
307
|
self.save_to.parent.mkdir(parents=True, exist_ok=True)
|
|
308
|
+
|
|
309
|
+
@property
|
|
310
|
+
def api_key(self) -> str:
|
|
311
|
+
"""
|
|
312
|
+
Get the API key (masked for security).
|
|
313
|
+
|
|
314
|
+
Returns
|
|
315
|
+
-------
|
|
316
|
+
str
|
|
317
|
+
Masked API key string.
|
|
318
|
+
"""
|
|
319
|
+
if hasattr(self, '_api_key') and self._api_key:
|
|
320
|
+
return f"sk-...{self._api_key[-4:]}" if len(self._api_key) > 4 else "***"
|
|
321
|
+
return ""
|
|
208
322
|
|
|
209
323
|
def message(self, msg: str) -> str:
|
|
210
|
-
"""
|
|
324
|
+
"""
|
|
325
|
+
Send a message to ChatGPT and get a response.
|
|
211
326
|
|
|
212
327
|
This method calls OpenAI's Chat Completions API via HTTP request.
|
|
213
328
|
|
|
214
|
-
|
|
215
|
-
|
|
329
|
+
Parameters
|
|
330
|
+
----------
|
|
331
|
+
msg : str
|
|
332
|
+
The message to be sent to ChatGPT.
|
|
216
333
|
|
|
217
|
-
Returns
|
|
334
|
+
Returns
|
|
335
|
+
-------
|
|
336
|
+
str
|
|
218
337
|
The response text from ChatGPT.
|
|
338
|
+
|
|
339
|
+
Raises
|
|
340
|
+
------
|
|
341
|
+
Exception
|
|
342
|
+
If the API request fails or returns an error.
|
|
343
|
+
|
|
344
|
+
Examples
|
|
345
|
+
--------
|
|
346
|
+
>>> chatgpt = ChatGPT()
|
|
347
|
+
>>> response = chatgpt.message("What is the capital of France?")
|
|
348
|
+
>>> print(response)
|
|
349
|
+
The capital of France is Paris.
|
|
219
350
|
"""
|
|
220
351
|
self.conversation.append({"role": "user", "content": msg})
|
|
221
352
|
|
|
@@ -233,7 +364,7 @@ class ChatGPT(ChatGPTModel):
|
|
|
233
364
|
}
|
|
234
365
|
|
|
235
366
|
headers = {
|
|
236
|
-
"Authorization": f"Bearer {self.
|
|
367
|
+
"Authorization": f"Bearer {self._api_key}",
|
|
237
368
|
"Content-Type": "application/json"
|
|
238
369
|
}
|
|
239
370
|
|
|
@@ -266,13 +397,27 @@ class ChatGPT(ChatGPTModel):
|
|
|
266
397
|
self,
|
|
267
398
|
save_as: Union[str, Path] = None
|
|
268
399
|
) -> int:
|
|
269
|
-
"""
|
|
400
|
+
"""
|
|
401
|
+
Save the current conversation to a JSON file.
|
|
270
402
|
|
|
271
|
-
|
|
272
|
-
|
|
403
|
+
Parameters
|
|
404
|
+
----------
|
|
405
|
+
save_as : Union[str, Path], optional
|
|
406
|
+
Path to save the conversation. If not provided, generates
|
|
407
|
+
a timestamped filename in the default conversations directory.
|
|
273
408
|
|
|
274
|
-
Returns
|
|
275
|
-
|
|
409
|
+
Returns
|
|
410
|
+
-------
|
|
411
|
+
int
|
|
412
|
+
0 on success, 1 on failure.
|
|
413
|
+
|
|
414
|
+
Examples
|
|
415
|
+
--------
|
|
416
|
+
>>> chatgpt = ChatGPT()
|
|
417
|
+
>>> chatgpt.message("Hello")
|
|
418
|
+
>>> result = chatgpt.save("my_conversation.json")
|
|
419
|
+
>>> print(result)
|
|
420
|
+
0
|
|
276
421
|
"""
|
|
277
422
|
if save_as is None:
|
|
278
423
|
save_as = Path(
|
|
@@ -40,9 +40,9 @@ if not dotenv_path.exists():
|
|
|
40
40
|
load_dotenv(dotenv_path)
|
|
41
41
|
|
|
42
42
|
|
|
43
|
-
# Getting API keys from environment
|
|
44
|
-
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
|
|
45
|
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
43
|
+
# Getting API keys from environment - masked for security
|
|
44
|
+
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY", "")
|
|
45
|
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
# Get LLM conversation directories.
|
|
@@ -63,7 +63,14 @@ CHATGPT_CONVERSATIONS_DIR = Path(
|
|
|
63
63
|
|
|
64
64
|
class LLM(Protocol):
|
|
65
65
|
"""
|
|
66
|
-
|
|
66
|
+
Protocol defining the attributes required for LLM conformance.
|
|
67
|
+
|
|
68
|
+
Attributes
|
|
69
|
+
----------
|
|
70
|
+
api_key : str
|
|
71
|
+
The API key for the LLM service.
|
|
72
|
+
name : str
|
|
73
|
+
The name of the LLM model.
|
|
67
74
|
"""
|
|
68
75
|
|
|
69
76
|
api_key: str
|
|
@@ -72,24 +79,67 @@ class LLM(Protocol):
|
|
|
72
79
|
|
|
73
80
|
@dataclass
|
|
74
81
|
class ClaudeModel:
|
|
75
|
-
|
|
82
|
+
"""
|
|
83
|
+
Data class representing a Claude model configuration.
|
|
84
|
+
|
|
85
|
+
Parameters
|
|
86
|
+
----------
|
|
87
|
+
api_key : str, optional
|
|
88
|
+
The Anthropic API key. Retrieved from environment if not provided.
|
|
89
|
+
model : str, optional
|
|
90
|
+
The Claude model identifier, by default "claude-3-5-sonnet-20241022".
|
|
91
|
+
name : str, optional
|
|
92
|
+
The model name, by default "Claude".
|
|
93
|
+
|
|
94
|
+
Attributes
|
|
95
|
+
----------
|
|
96
|
+
api_key : str
|
|
97
|
+
The Anthropic API key.
|
|
98
|
+
model : str
|
|
99
|
+
The Claude model identifier.
|
|
100
|
+
name : str
|
|
101
|
+
The model name.
|
|
102
|
+
"""
|
|
103
|
+
api_key: str = "" # Will be set from environment in __post_init__
|
|
76
104
|
model: str = "claude-3-5-sonnet-20241022"
|
|
77
105
|
name: str = "Claude"
|
|
106
|
+
|
|
107
|
+
def __post_init__(self):
|
|
108
|
+
"""Initialize API key from environment if not provided."""
|
|
109
|
+
if not self.api_key:
|
|
110
|
+
self.api_key = ANTHROPIC_API_KEY
|
|
78
111
|
|
|
79
112
|
@staticmethod
|
|
80
113
|
def load_conversation(filepath: Union[str, Path]) -> List[Dict[str, str]]:
|
|
81
114
|
"""
|
|
82
|
-
Load a previous conversation from a JSON file
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
115
|
+
Load a previous conversation from a JSON file.
|
|
116
|
+
|
|
117
|
+
Parameters
|
|
118
|
+
----------
|
|
119
|
+
filepath : Union[str, Path]
|
|
120
|
+
Path to the conversation JSON file. If relative, will be resolved
|
|
121
|
+
relative to the Claude conversations directory.
|
|
122
|
+
|
|
123
|
+
Returns
|
|
124
|
+
-------
|
|
125
|
+
List[Dict[str, str]]
|
|
126
|
+
List of message dictionaries containing the conversation history.
|
|
127
|
+
Each dictionary has 'role' and 'content' keys.
|
|
128
|
+
|
|
129
|
+
Raises
|
|
130
|
+
------
|
|
131
|
+
FileNotFoundError
|
|
132
|
+
If the specified file doesn't exist.
|
|
133
|
+
json.JSONDecodeError
|
|
134
|
+
If the file contains invalid JSON.
|
|
135
|
+
ValueError
|
|
136
|
+
If the conversation format is invalid.
|
|
137
|
+
|
|
138
|
+
Examples
|
|
139
|
+
--------
|
|
140
|
+
>>> messages = ClaudeModel.load_conversation("my_chat.json")
|
|
141
|
+
>>> print(len(messages))
|
|
142
|
+
5
|
|
93
143
|
"""
|
|
94
144
|
filepath = Path(filepath)
|
|
95
145
|
if not filepath.is_absolute():
|
|
@@ -133,24 +183,67 @@ class ClaudeModel:
|
|
|
133
183
|
|
|
134
184
|
@dataclass
|
|
135
185
|
class ChatGPTModel:
|
|
136
|
-
|
|
186
|
+
"""
|
|
187
|
+
Data class representing a ChatGPT model configuration.
|
|
188
|
+
|
|
189
|
+
Parameters
|
|
190
|
+
----------
|
|
191
|
+
api_key : str, optional
|
|
192
|
+
The OpenAI API key. Retrieved from environment if not provided.
|
|
193
|
+
model : str, optional
|
|
194
|
+
The GPT model identifier, by default "gpt-4".
|
|
195
|
+
name : str, optional
|
|
196
|
+
The model name, by default "ChatGPT".
|
|
197
|
+
|
|
198
|
+
Attributes
|
|
199
|
+
----------
|
|
200
|
+
api_key : str
|
|
201
|
+
The OpenAI API key.
|
|
202
|
+
model : str
|
|
203
|
+
The GPT model identifier.
|
|
204
|
+
name : str
|
|
205
|
+
The model name.
|
|
206
|
+
"""
|
|
207
|
+
api_key: str = "" # Will be set from environment in __post_init__
|
|
137
208
|
model: str = "gpt-4"
|
|
138
209
|
name: str = "ChatGPT"
|
|
210
|
+
|
|
211
|
+
def __post_init__(self):
|
|
212
|
+
"""Initialize API key from environment if not provided."""
|
|
213
|
+
if not self.api_key:
|
|
214
|
+
self.api_key = OPENAI_API_KEY
|
|
139
215
|
|
|
140
216
|
@staticmethod
|
|
141
217
|
def load_conversation(filepath: Union[str, Path]) -> List[Dict[str, str]]:
|
|
142
218
|
"""
|
|
143
|
-
Load a previous conversation from a JSON file
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
219
|
+
Load a previous conversation from a JSON file.
|
|
220
|
+
|
|
221
|
+
Parameters
|
|
222
|
+
----------
|
|
223
|
+
filepath : Union[str, Path]
|
|
224
|
+
Path to the conversation JSON file. If relative, will be resolved
|
|
225
|
+
relative to the ChatGPT conversations directory.
|
|
226
|
+
|
|
227
|
+
Returns
|
|
228
|
+
-------
|
|
229
|
+
List[Dict[str, str]]
|
|
230
|
+
List of message dictionaries containing the conversation history.
|
|
231
|
+
Each dictionary has 'role' and 'content' keys.
|
|
232
|
+
|
|
233
|
+
Raises
|
|
234
|
+
------
|
|
235
|
+
FileNotFoundError
|
|
236
|
+
If the specified file doesn't exist.
|
|
237
|
+
json.JSONDecodeError
|
|
238
|
+
If the file contains invalid JSON.
|
|
239
|
+
ValueError
|
|
240
|
+
If the conversation format is invalid.
|
|
241
|
+
|
|
242
|
+
Examples
|
|
243
|
+
--------
|
|
244
|
+
>>> messages = ChatGPTModel.load_conversation("my_chat.json")
|
|
245
|
+
>>> print(len(messages))
|
|
246
|
+
3
|
|
154
247
|
"""
|
|
155
248
|
filepath = Path(filepath)
|
|
156
249
|
if not filepath.is_absolute():
|
|
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
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|