refactor: move LLM provider implementations to literate skill notes
This commit is contained in:
@@ -35,53 +35,45 @@ Define the interface for reliable communication with the Anthropic Messages API.
|
||||
:STATUS: SIGNED
|
||||
:END:
|
||||
|
||||
* Phase B: Blueprint (PROTOCOL)
|
||||
** 2. Semantic Interfaces
|
||||
|
||||
** 1. Architectural Intent
|
||||
*** `execute-anthropic-request`
|
||||
:signature `(execute-anthropic-request prompt system-prompt &key model) :string`
|
||||
:description "Direct call to the Anthropic Messages API."
|
||||
|
||||
The Anthropic Provider Agent will be designed as a stateless service responsible for managing communication with the Anthropic API. It will abstract away the complexities of API authentication, model selection, prompt construction, and response parsing, exposing a simple, functional interface to the broader PSF. The key architectural constraint is reliability and efficient context management. Error handling and API rate limiting must be gracefully managed to prevent cascading failures.
|
||||
* Phase D: Build (Implementation)
|
||||
|
||||
** 2. Semantic Interfaces (Lisp Signatures)
|
||||
#+begin_src lisp :tangle ../projects/org-skill-provider-anthropic/src/provider-logic.lisp
|
||||
(in-package :org-agent)
|
||||
|
||||
*** `anthropic-query`
|
||||
(defun execute-anthropic-request (prompt system-prompt &key model)
|
||||
(let ((api-key (uiop:getenv "ANTHROPIC_API_KEY"))
|
||||
(endpoint "https://api.anthropic.com/v1/messages")
|
||||
(model-id (or model "claude-3-5-sonnet-20240620")))
|
||||
(unless api-key (return-from execute-anthropic-request "(:type :LOG :payload (:text \"Anthropic API Key missing\"))"))
|
||||
(let* ((headers `(("Content-Type" . "application/json")
|
||||
("x-api-key" . ,api-key)
|
||||
("anthropic-version" . "2023-06-01")))
|
||||
(body (cl-json:encode-json-to-string
|
||||
`((model . ,model-id)
|
||||
(max_tokens . 4096)
|
||||
(system . ,system-prompt)
|
||||
(messages . (( (role . "user") (content . ,prompt) )))))))
|
||||
(handler-case (let* ((response (dex:post endpoint :headers headers :content body :connect-timeout 10 :read-timeout 30))
|
||||
(json (cl-json:decode-json-from-string response)))
|
||||
;; Anthropic response structure: content[0].text
|
||||
(cdr (assoc :text (car (cdr (assoc :content json))))))
|
||||
(error (c) (format nil "(:type :LOG :payload (:text \"Anthropic Failure: ~a\"))" c))))))
|
||||
#+end_src
|
||||
|
||||
This function is the primary entry point for interacting with the Anthropic models. It constructs the full prompt (system prompt + user query), sends it to the Anthropic API, and returns the model's response.
|
||||
|
||||
:lisp
|
||||
(defun anthropic-query (query &key model system-prompt max-tokens temperature top-p stream?)
|
||||
"Sends a query to the Anthropic API and returns the response.
|
||||
|
||||
QUERY: The user's question or instruction (string).
|
||||
MODEL: (Optional) The Claude model to use (string, default 'claude-v1.3').
|
||||
SYSTEM-PROMPT: (Optional) A system prompt to guide the model (string).
|
||||
MAX-TOKENS: (Optional) The maximum number of tokens in the response (integer).
|
||||
TEMPERATURE: (Optional) Sampling temperature (float, 0.0-1.0).
|
||||
TOP-P: (Optional) Top-p sampling (float, 0.0-1.0).
|
||||
STREAM?: (Optional) Whether to stream the response (boolean, default nil).
|
||||
|
||||
Returns: A string containing the model's generated response, or nil on error."
|
||||
...)
|
||||
|
||||
*** `anthropic-authenticate`
|
||||
|
||||
This function handles API key authentication. It retrieves the API key from environment variables or a configuration file.
|
||||
|
||||
:lisp
|
||||
(defun anthropic-authenticate ()
|
||||
"Authenticates with the Anthropic API, retrieving the API key from the environment or config.
|
||||
|
||||
Returns: The API key (string), or nil on failure."
|
||||
...)
|
||||
|
||||
*** `anthropic-model-resolve`
|
||||
|
||||
This function resolves a symbolic model name (e.g., 'most-powerful') to a specific Claude model identifier (e.g., 'claude-v1.3-100k').
|
||||
|
||||
:lisp
|
||||
(defun anthropic-model-resolve (model-symbol)
|
||||
"Resolves a symbolic model name to a specific Anthropic model identifier.
|
||||
|
||||
MODEL-SYMBOL: A symbolic name for the model (symbol).
|
||||
|
||||
Returns: The Anthropic model identifier (string), or nil if resolution fails."
|
||||
...)
|
||||
* Registration
|
||||
#+begin_src lisp
|
||||
(progn
|
||||
(org-agent:register-neuro-backend :anthropic #'execute-anthropic-request)
|
||||
|
||||
(defskill :skill-provider-anthropic
|
||||
:priority 110
|
||||
:trigger (lambda (context) nil)
|
||||
:neuro (lambda (context) nil)
|
||||
:symbolic (lambda (action context) action)))
|
||||
#+end_src
|
||||
|
||||
Reference in New Issue
Block a user