gnarly 0.0.0.dev9__tar.gz → 0.0.0.dev11__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.

Files changed (42) hide show
  1. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/PKG-INFO +1 -1
  2. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/source/conf.py +2 -0
  3. gnarly-0.0.0.dev11/jupyter.log +3 -0
  4. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/pyproject.toml +1 -1
  5. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/src/gnarly/utils/code/projects.py +1 -1
  6. gnarly-0.0.0.dev11/src/gnarly/utils/llm/claude.py +98 -0
  7. gnarly-0.0.0.dev11/src/gnarly/utils/llm/interfaces.py +438 -0
  8. gnarly-0.0.0.dev11/src/gnarly/utils/llm/models.py +285 -0
  9. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/uv.lock +1 -1
  10. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/.gitignore +0 -0
  11. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/.python-version +0 -0
  12. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/README.md +0 -0
  13. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/Makefile +0 -0
  14. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/api/gnarly.rst +0 -0
  15. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/api/gnarly.utils.code.rst +0 -0
  16. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/api/gnarly.utils.llm.rst +0 -0
  17. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/api/gnarly.utils.rst +0 -0
  18. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/api/modules.rst +0 -0
  19. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/make.bat +0 -0
  20. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/source/api/gnarly.rst +0 -0
  21. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/source/api/gnarly.utils.code.rst +0 -0
  22. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/source/api/gnarly.utils.llm.rst +0 -0
  23. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/source/api/gnarly.utils.rst +0 -0
  24. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/source/api/modules.rst +0 -0
  25. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/source/api.md +0 -0
  26. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/source/index.md +0 -0
  27. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/docs/source/usage.md +0 -0
  28. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/src/gnarly/__init__.py +0 -0
  29. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/src/gnarly/constants.py +0 -0
  30. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/src/gnarly/errors.py +0 -0
  31. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/src/gnarly/types.py +0 -0
  32. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/src/gnarly/utils/__init__.py +0 -0
  33. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/src/gnarly/utils/code/__init__.py +0 -0
  34. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/src/gnarly/utils/io.py +0 -0
  35. /gnarly-0.0.0.dev9/src/gnarly/utils/llm/claude.py → /gnarly-0.0.0.dev11/src/gnarly/utils/llm/.ipynb_checkpoints/claude-checkpoint.py +0 -0
  36. /gnarly-0.0.0.dev9/src/gnarly/utils/llm/interfaces.py → /gnarly-0.0.0.dev11/src/gnarly/utils/llm/.ipynb_checkpoints/interfaces-checkpoint.py +0 -0
  37. /gnarly-0.0.0.dev9/src/gnarly/utils/llm/models.py → /gnarly-0.0.0.dev11/src/gnarly/utils/llm/.ipynb_checkpoints/models-checkpoint.py +0 -0
  38. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/src/gnarly/utils/llm/__init__.py +0 -0
  39. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/src/gnarly/utils/llm/chatgpt.py +0 -0
  40. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/tests/__init__.py +0 -0
  41. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/tests/test_constants.py +0 -0
  42. {gnarly-0.0.0.dev9 → gnarly-0.0.0.dev11}/tests/test_io_utilities.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gnarly
3
- Version: 0.0.0.dev9
3
+ Version: 0.0.0.dev11
4
4
  Summary: A sophisticated, general-use abstraction layer for the lazy.
5
5
  Author-email: "K. LeBryce" <k.lebryce@pm.me>
6
6
  Requires-Python: >=3.10
@@ -68,6 +68,8 @@ autodoc_default_options = {
68
68
  'members': True,
69
69
  'undoc-members': True,
70
70
  'show-inheritance': True,
71
+ 'private-members': False,
72
+ 'special-members': False,
71
73
  }
72
74
 
73
75
  # Napoleon configuration (for Google/NumPy style docstrings)
@@ -0,0 +1,3 @@
1
+ nohup: ignoring input
2
+ error: Failed to spawn: `jupyter`
3
+ Caused by: No such file or directory (os error 2)
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "gnarly"
3
- version = "0.0.0dev9"
3
+ version = "0.0.0dev11"
4
4
  description = "A sophisticated, general-use abstraction layer for the lazy."
5
5
  readme = "README.md"
6
6
  authors = [{ name = "K. LeBryce", email = "k.lebryce@pm.me" }]
@@ -134,7 +134,7 @@ class Project(GnarlyNamespace):
134
134
  logger.info(f"Created {p}")
135
135
 
136
136
  core_package_dir: Path = root_path / f"src/{name}"
137
- core_package_dir.mkdiir(parents=True, exist_ok=True)
137
+ core_package_dir.mkdir(parents=True, exist_ok=True)
138
138
 
139
139
 
140
140
  return Project.create(
@@ -0,0 +1,98 @@
1
+ """Claude REPL
2
+
3
+ Entry point for the Claude REPL.
4
+ """
5
+ import logging
6
+ from datetime import datetime as dt
7
+ from pathlib import Path
8
+
9
+ from ..io import stream
10
+ from .interfaces import Claude
11
+ from ..llm.models import CLAUDE_CONVERSATIONS_DIR, ClaudeModel
12
+
13
+
14
+ logger = logging.getLogger(__name__)
15
+ logging.basicConfig(level=logging.INFO)
16
+
17
+ pp = stream
18
+
19
+ # Helper functions
20
+ def check_for_jupyter() -> bool:
21
+ """
22
+ check_for_jupyter
23
+
24
+ Evaluates whether the REPL is being invoked within a Jupyter environment.
25
+ """
26
+ try:
27
+ shell = get_ipython().__class__.__name__
28
+ if shell == 'ZMQInteractiveShell': # Jupyter notebook or qtconsole
29
+ return True
30
+ elif shell == 'TerminalInteractiveShell': # Terminal IPython
31
+ return False
32
+ else:
33
+ return False
34
+ except NameError: # Likely standard Python interpreter
35
+ return False
36
+
37
+ def main() -> bool:
38
+ """
39
+ main
40
+
41
+ Runloop for the Claude REPL.
42
+ """
43
+ exit_commands: list = ["quit", "exit", "q", "e", "x"]
44
+ save_command: str = "save"
45
+ load_command: str = "load"
46
+ is_jupyter = check_for_jupyter()
47
+
48
+ # Instantiate the Claude interface
49
+ claude = Claude()
50
+
51
+ try:
52
+ while True:
53
+ pp("[You]") # No newline after [You]
54
+ prompt = input() # Get input on same line
55
+
56
+ if is_jupyter:
57
+ pp("\n\n[You]")
58
+ pp(prompt)
59
+ pp("\n")
60
+
61
+ # Check for exit command
62
+ if prompt.lower() in exit_commands:
63
+ pp("Goodbye!")
64
+ return True
65
+
66
+ # Check for save command
67
+ if prompt.lower() == save_command:
68
+ claude.save(save_as=Path(
69
+ f"{CLAUDE_CONVERSATIONS_DIR}/{dt.now().strftime('%Y-%m-%d_%H%M%S')}.json"
70
+ ))
71
+ pp("Conversation saved!")
72
+ continue
73
+
74
+ # Check for load command
75
+ if prompt.lower().startswith(load_command):
76
+ try:
77
+ # Extract the filepath from the command
78
+ filepath = prompt[len(load_command):].strip().strip("'\"")
79
+ if filepath:
80
+ # Load the conversation
81
+ claude.conversation = ClaudeModel.load_conversation(filepath)
82
+ pp(f"Loaded conversation from {filepath}")
83
+ continue
84
+ else:
85
+ pp("Please provide a filepath to load")
86
+ continue
87
+ except Exception as e:
88
+ pp(f"Error loading conversation: {e}")
89
+ continue
90
+
91
+ response = claude.message(prompt)[0]["content"]
92
+ pp("\n\n[Claude]")
93
+ pp(response)
94
+ pp("\n")
95
+
96
+ except KeyboardInterrupt:
97
+ pp("Goodbye!")
98
+ return True
@@ -0,0 +1,438 @@
1
+ """LLM Interfaces
2
+
3
+ Classes for interfacing with LLMs as defined in the `models` module.
4
+ """
5
+ # ─── import statements ───────────────────────────────────────────────── ✦✦ ──
6
+
7
+ # standard library imports
8
+ import json
9
+ import logging
10
+ import os
11
+ import urllib.request
12
+ import urllib.parse
13
+ from datetime import datetime as dt
14
+ from pathlib import Path
15
+ from typing import Dict, List, Tuple, TYPE_CHECKING, Union
16
+ if TYPE_CHECKING:
17
+ from _typeshed import SupportsWrite
18
+
19
+ # third-party imports
20
+ from dotenv import load_dotenv
21
+ from platformdirs import user_config_path
22
+
23
+ # local imports
24
+ from .models import (
25
+ CHATGPT_CONVERSATIONS_DIR, CLAUDE_CONVERSATIONS_DIR,
26
+ ClaudeModel, ChatGPTModel
27
+ )
28
+
29
+ # ─── logger setup ──────────────────────────────────────────────────────── ✦ ─
30
+ logger = logging.getLogger(__name__)
31
+ logging.basicConfig(level=logging.INFO)
32
+
33
+ # ─── constants ─────────────────────────────────────────────────────────── ✦ ─
34
+ APP_NAME = "gnarly"
35
+ APP_AUTHOR = "k.lebryce"
36
+ APP_CONFIG_DIR: Path = user_config_path(
37
+ APP_NAME, APP_AUTHOR, ensure_exists=True
38
+ )
39
+
40
+ # load environment variables from .env file
41
+ try:
42
+ load_dotenv(Path(APP_CONFIG_DIR, ".env").resolve())
43
+ except FileNotFoundError:
44
+ APP_CONFIG_DIR.mkdir(parents=True, exist_ok=True)
45
+ Path(APP_CONFIG_DIR, ".env").touch()
46
+ except IOError as e:
47
+ raise IOError(
48
+ "Failed to load environment variables from .env file."
49
+ ) from e
50
+
51
+
52
+ # ─── interfaces ──────────────────────────────────────────────────────── ✦✦ ──
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
+
94
+ def __init__(
95
+ self,
96
+ system_prompt: str = "You are Claude, a friendly AI assistant."
97
+ ):
98
+ super().__init__()
99
+ self._api_key = self.api_key or os.getenv("ANTHROPIC_API_KEY", "")
100
+ if not self._api_key:
101
+ raise ValueError("ANTHROPIC_API_KEY environment variable is required")
102
+
103
+ self.conversation: List[Dict] = []
104
+ self.max_tokens: int = 4096
105
+ self.system_prompt = system_prompt
106
+ self.temperature: float = 0.2
107
+
108
+ # Ensure save_to is a Path object
109
+ self.save_to = Path(CLAUDE_CONVERSATIONS_DIR)
110
+
111
+ # Create directory if it doesn't exist
112
+ self.save_to.parent.mkdir(parents=True, exist_ok=True)
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 ""
128
+
129
+ def message(self, msg: str) -> Tuple[Dict, Dict]:
130
+ """
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
156
+ """
157
+ self.conversation.append({"role": "user", "content": msg})
158
+
159
+ # Prepare the API request
160
+ url = "https://api.anthropic.com/v1/messages"
161
+
162
+ data = {
163
+ "model": self.model,
164
+ "max_tokens": self.max_tokens,
165
+ "system": self.system_prompt,
166
+ "temperature": self.temperature,
167
+ "messages": self.conversation
168
+ }
169
+
170
+ headers = {
171
+ "x-api-key": self._api_key,
172
+ "Content-Type": "application/json",
173
+ "anthropic-version": "2023-06-01"
174
+ }
175
+
176
+ # Make the HTTP request
177
+ req = urllib.request.Request(
178
+ url,
179
+ data=json.dumps(data).encode("utf-8"),
180
+ headers=headers
181
+ )
182
+
183
+ try:
184
+ with urllib.request.urlopen(req) as response:
185
+ result = json.loads(response.read().decode("utf-8"))
186
+ except urllib.error.HTTPError as e:
187
+ error_body = e.read().decode("utf-8")
188
+ logger.error(f"API request failed: {e.code} {e.reason} - {error_body}")
189
+ raise Exception(f"Claude API error: {e.code} {e.reason}")
190
+ except Exception as e:
191
+ logger.error(f"Request failed: {str(e)}")
192
+ raise
193
+
194
+ # Extract response content
195
+ response_content = result["content"][0]["text"]
196
+ response = {"role": "assistant", "content": response_content}
197
+ self.conversation.append(response)
198
+
199
+ return response, result
200
+
201
+ def save(self, save_as: Union[str, Path] = None) -> int:
202
+ """
203
+ Save the current conversation to a JSON file.
204
+
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
224
+ """
225
+ try:
226
+ target_path = Path(save_as) if save_as else self.save_to
227
+ if not target_path.is_absolute():
228
+ target_path = Path(CLAUDE_CONVERSATIONS_DIR) / target_path
229
+
230
+ target_path.parent.mkdir(parents=True, exist_ok=True)
231
+
232
+ with open(target_path, "w") as f:
233
+ json.dump(self.conversation, f, indent=4)
234
+ logger.info(f"Successfully saved conversation to {target_path}")
235
+ return 0
236
+
237
+ except Exception as e:
238
+ logger.error(f"Failed to save conversation: {str(e)}")
239
+ return 1
240
+
241
+
242
+ class ChatGPT(ChatGPTModel):
243
+ """
244
+ A turn-based, chat-like interface for the OpenAI API.
245
+
246
+ This class provides a conversational interface to ChatGPT models,
247
+ maintaining conversation history and handling API communication
248
+ via HTTP requests.
249
+
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?
287
+ """
288
+
289
+ def __init__(
290
+ self,
291
+ model: str = "gpt-4",
292
+ instructions: str = "You are ChatGPT, a helpful AI assistant."
293
+ ) -> None:
294
+ super().__init__()
295
+ self._api_key = self.api_key or os.getenv("OPENAI_API_KEY", "")
296
+ if not self._api_key:
297
+ raise ValueError("OPENAI_API_KEY environment variable is required")
298
+
299
+ self.conversation: List[Dict] = []
300
+ self.model: str = model
301
+ self.instructions: str = instructions
302
+ self.max_tokens: int = 4096
303
+ self.save_to: Path = Path(CHATGPT_CONVERSATIONS_DIR)
304
+ self.temperature: float = 0.2
305
+
306
+ # Ensure that the conversations directory exists.
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 ""
322
+
323
+ def message(self, msg: str) -> str:
324
+ """
325
+ Send a message to ChatGPT and get a response.
326
+
327
+ This method calls OpenAI's Chat Completions API via HTTP request.
328
+
329
+ Parameters
330
+ ----------
331
+ msg : str
332
+ The message to be sent to ChatGPT.
333
+
334
+ Returns
335
+ -------
336
+ str
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.
350
+ """
351
+ self.conversation.append({"role": "user", "content": msg})
352
+
353
+ # Prepare messages for API call
354
+ messages = [{"role": "system", "content": self.instructions}] + self.conversation
355
+
356
+ # Prepare the API request
357
+ url = "https://api.openai.com/v1/chat/completions"
358
+
359
+ data = {
360
+ "model": self.model,
361
+ "messages": messages,
362
+ "max_tokens": self.max_tokens,
363
+ "temperature": self.temperature
364
+ }
365
+
366
+ headers = {
367
+ "Authorization": f"Bearer {self._api_key}",
368
+ "Content-Type": "application/json"
369
+ }
370
+
371
+ # Make the HTTP request
372
+ req = urllib.request.Request(
373
+ url,
374
+ data=json.dumps(data).encode("utf-8"),
375
+ headers=headers
376
+ )
377
+
378
+ try:
379
+ with urllib.request.urlopen(req) as response:
380
+ result = json.loads(response.read().decode("utf-8"))
381
+ except urllib.error.HTTPError as e:
382
+ error_body = e.read().decode("utf-8")
383
+ logger.error(f"API request failed: {e.code} {e.reason} - {error_body}")
384
+ raise Exception(f"OpenAI API error: {e.code} {e.reason}")
385
+ except Exception as e:
386
+ logger.error(f"Request failed: {str(e)}")
387
+ raise
388
+
389
+ # Extract response content
390
+ response_text = result["choices"][0]["message"]["content"]
391
+ response = {"role": "assistant", "content": response_text}
392
+ self.conversation.append(response)
393
+
394
+ return response_text
395
+
396
+ def save(
397
+ self,
398
+ save_as: Union[str, Path] = None
399
+ ) -> int:
400
+ """
401
+ Save the current conversation to a JSON file.
402
+
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.
408
+
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
421
+ """
422
+ if save_as is None:
423
+ save_as = Path(
424
+ f"~/.llm/chatgpt/conversations/{dt.now().strftime('%Y-%m-%d_%H-%M-%S')}.json"
425
+ ).expanduser()
426
+
427
+ try:
428
+ target_path = Path(save_as)
429
+ target_path.parent.mkdir(parents=True, exist_ok=True)
430
+
431
+ with open(target_path, "w") as f:
432
+ logger.info(f"Saving conversation to {target_path}")
433
+ json.dump(self.conversation, f, indent=4)
434
+ logger.info("Success.")
435
+ return 0
436
+ except Exception as e:
437
+ logger.error(f"Error: Failed to save conversation: {str(e)}")
438
+ return 1
@@ -0,0 +1,285 @@
1
+ """
2
+ LLM Models
3
+ ==========
4
+
5
+ Classes for representing LLM models and their characteristic attributes.
6
+
7
+ """
8
+
9
+ # Standard library imports: Package-level
10
+ import json
11
+ import logging
12
+ import os
13
+ from dataclasses import dataclass
14
+ from enum import Enum
15
+ from pathlib import Path
16
+ from typing import Any, Dict, List, Protocol, Union
17
+
18
+ # Third-party imports: Module-level
19
+ from dotenv import load_dotenv
20
+
21
+
22
+ # Set up logging.
23
+ logger = logging.getLogger(__name__)
24
+ logging.basicConfig(level=logging.INFO)
25
+
26
+
27
+ # Create LLM data directories as needed.
28
+ gnarly_dir = Path("~/Library/Mobile Documents/com~apple~CloudDocs/gnarly").expanduser()
29
+ llm_directory = gnarly_dir / "llm"
30
+ dotenv_path = gnarly_dir / ".env"
31
+ if not gnarly_dir.exists():
32
+ gnarly_dir.mkdir(parents=True, exist_ok=True)
33
+ if not llm_directory.exists():
34
+ llm_directory.mkdir(parents=True, exist_ok=True)
35
+ if not dotenv_path.exists():
36
+ dotenv_path.touch(exist_ok=True)
37
+
38
+
39
+ # Load environment variables.
40
+ load_dotenv(dotenv_path)
41
+
42
+
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
+
47
+
48
+ # Get LLM conversation directories.
49
+ CLAUDE_CONVERSATIONS_DIR = Path(
50
+ os.getenv(
51
+ "CLAUDE_CONVERSATIONS_DIR",
52
+ str(llm_directory / "claude/conversations")
53
+ )
54
+ )
55
+
56
+ CHATGPT_CONVERSATIONS_DIR = Path(
57
+ os.getenv(
58
+ "CHATGPT_CONVERSATIONS_DIR",
59
+ str(llm_directory / "chatgpt/conversations")
60
+ )
61
+ )
62
+
63
+
64
+ class LLM(Protocol):
65
+ """
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.
74
+ """
75
+
76
+ api_key: str
77
+ name: str
78
+
79
+
80
+ @dataclass
81
+ class ClaudeModel:
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__
104
+ model: str = "claude-3-5-sonnet-20241022"
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
111
+
112
+ @staticmethod
113
+ def load_conversation(filepath: Union[str, Path]) -> List[Dict[str, str]]:
114
+ """
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
143
+ """
144
+ filepath = Path(filepath)
145
+ if not filepath.is_absolute():
146
+ filepath = Path(CLAUDE_CONVERSATIONS_DIR) / filepath
147
+
148
+ if not filepath.exists():
149
+ filepath.parent.mkdir(parents=True, exist_ok=True)
150
+ filepath.touch(exist_ok=True)
151
+
152
+ try:
153
+ with open(filepath, "r") as f:
154
+ content = f.read().strip()
155
+ if not content:
156
+ return []
157
+ conversation = json.loads(content)
158
+
159
+ # Validate conversation format
160
+ if not isinstance(conversation, list):
161
+ raise ValueError("Conversation must be a list of messages")
162
+
163
+ for msg in conversation:
164
+ if (
165
+ not isinstance(msg, dict)
166
+ or "role" not in msg
167
+ or "content" not in msg
168
+ ):
169
+ raise ValueError("Invalid message format in conversation")
170
+
171
+ return conversation
172
+
173
+ except FileNotFoundError:
174
+ logger.error(f"Conversation file not found: {filepath}")
175
+ raise
176
+ except json.JSONDecodeError as e:
177
+ logger.error(f"Invalid JSON in conversation file: {filepath}")
178
+ raise
179
+ except Exception as e:
180
+ logger.error(f"Error loading conversation: {str(e)}")
181
+ raise
182
+
183
+
184
+ @dataclass
185
+ class ChatGPTModel:
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__
208
+ model: str = "gpt-4"
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
215
+
216
+ @staticmethod
217
+ def load_conversation(filepath: Union[str, Path]) -> List[Dict[str, str]]:
218
+ """
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
247
+ """
248
+ filepath = Path(filepath)
249
+ if not filepath.is_absolute():
250
+ filepath = Path(CHATGPT_CONVERSATIONS_DIR) / filepath
251
+
252
+ if not filepath.exists():
253
+ filepath.parent.mkdir(parents=True, exist_ok=True)
254
+ filepath.touch(exist_ok=True)
255
+
256
+ try:
257
+ with open(filepath, "r") as f:
258
+ content = f.read().strip()
259
+ if not content:
260
+ return []
261
+ conversation = json.loads(content)
262
+
263
+ # Validate conversation format
264
+ if not isinstance(conversation, list):
265
+ raise ValueError("Conversation must be a list of messages")
266
+
267
+ for msg in conversation:
268
+ if (
269
+ not isinstance(msg, dict)
270
+ or "role" not in msg
271
+ or "content" not in msg
272
+ ):
273
+ raise ValueError("Invalid message format in conversation")
274
+
275
+ return conversation
276
+
277
+ except FileNotFoundError:
278
+ logger.error(f"Conversation file not found: {filepath}")
279
+ raise
280
+ except json.JSONDecodeError as e:
281
+ logger.error(f"Invalid JSON in conversation file: {filepath}")
282
+ raise
283
+ except Exception as e:
284
+ logger.error(f"Error loading conversation: {str(e)}")
285
+ raise
@@ -29,7 +29,7 @@ wheels = [
29
29
 
30
30
  [[package]]
31
31
  name = "gnarly"
32
- version = "0.0.0.dev8"
32
+ version = "0.0.0.dev10"
33
33
  source = { editable = "." }
34
34
  dependencies = [
35
35
  { name = "python-dotenv" },
File without changes
File without changes
File without changes
File without changes