Exercise spell and NPC integration through native providers and published artifacts
This commit is contained in:
@@ -4,8 +4,9 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
type extractionResponse struct {
|
||||
@@ -27,16 +28,22 @@ type fakeSpellsLLMClient struct {
|
||||
response extractionResponse
|
||||
responses []extractionResponse
|
||||
rawResponses [][]byte
|
||||
requests []contracts.StructuredCompletionRequest
|
||||
requests []promptkit.GenerateRequest
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (client *fakeSpellsLLMClient) CompleteStructured(_ context.Context, req contracts.StructuredCompletionRequest, out any) (contracts.StructuredCompletionResponse, error) {
|
||||
client.requests = append(client.requests, cloneStructuredCompletionRequest(req))
|
||||
func (client *fakeSpellsLLMClient) Generate(ctx context.Context, req promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||
client.mu.Lock()
|
||||
defer client.mu.Unlock()
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client.requests = append(client.requests, req)
|
||||
var content []byte
|
||||
if client.rawResponses != nil {
|
||||
index := len(client.requests) - 1
|
||||
if index >= len(client.rawResponses) {
|
||||
return contracts.StructuredCompletionResponse{}, fmt.Errorf("missing fake response %d", index)
|
||||
return nil, fmt.Errorf("missing fake response %d", index)
|
||||
}
|
||||
content = append([]byte(nil), client.rawResponses[index]...)
|
||||
} else {
|
||||
@@ -44,20 +51,17 @@ func (client *fakeSpellsLLMClient) CompleteStructured(_ context.Context, req con
|
||||
if client.responses != nil {
|
||||
index := len(client.requests) - 1
|
||||
if index >= len(client.responses) {
|
||||
return contracts.StructuredCompletionResponse{}, fmt.Errorf("missing fake response %d", index)
|
||||
return nil, fmt.Errorf("missing fake response %d", index)
|
||||
}
|
||||
response = client.responses[index]
|
||||
}
|
||||
var err error
|
||||
content, err = json.Marshal(response)
|
||||
if err != nil {
|
||||
return contracts.StructuredCompletionResponse{}, err
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := json.Unmarshal(content, out); err != nil {
|
||||
return contracts.StructuredCompletionResponse{}, fmt.Errorf("populate structured target: %w", err)
|
||||
}
|
||||
return contracts.StructuredCompletionResponse{Content: content}, nil
|
||||
return &promptkit.GenerateResponse{Content: string(content)}, nil
|
||||
}
|
||||
|
||||
func responseSourceRefs(startUnitID int, endUnitID int) []spellSourceRefResponse {
|
||||
@@ -68,22 +72,3 @@ func responseSourceRefs(startUnitID int, endUnitID int) []spellSourceRefResponse
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func cloneStructuredCompletionRequest(req contracts.StructuredCompletionRequest) contracts.StructuredCompletionRequest {
|
||||
req.Inputs = req.Inputs.Clone()
|
||||
correction, err := contracts.CloneSemanticCorrection(req.Correction)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
req.Correction = correction
|
||||
if len(req.Vars) == 0 {
|
||||
req.Vars = nil
|
||||
return req
|
||||
}
|
||||
vars := make(map[string]any, len(req.Vars))
|
||||
for key, value := range req.Vars {
|
||||
vars[key] = value
|
||||
}
|
||||
req.Vars = vars
|
||||
return req
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user