Prototype ConfigKit and PromptKit adoption and record the findings
This commit is contained in:
61
examples/library-adoption/offline.go
Normal file
61
examples/library-adoption/offline.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const validProposal = `{"corrections":[{"id":1,"original_text":"hello world","corrected_text":"Hello, world.","confidence":0.95}]}`
|
||||
const validAssessment = `{"validations":[{"correction_index":0,"approved":true,"confidence":0.95,"reason":"Punctuation and capitalization only"}]}`
|
||||
|
||||
// roundTripFunc exercises PromptKit's built-in HTTP path without any network.
|
||||
type roundTripFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
||||
|
||||
func providerResponse(status int, content string, usage any) *http.Response {
|
||||
body := map[string]any{"choices": []any{map[string]any{"message": map[string]any{"role": "assistant", "content": content}, "finish_reason": "stop"}}}
|
||||
if status != http.StatusOK {
|
||||
body = map[string]any{"error": map[string]any{"message": "synthetic temporary failure"}}
|
||||
}
|
||||
if usage != nil {
|
||||
body["usage"] = usage
|
||||
}
|
||||
data, _ := json.Marshal(body)
|
||||
return &http.Response{StatusCode: status, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(string(data)))}
|
||||
}
|
||||
|
||||
func demonstrationClient() *http.Client {
|
||||
var mu sync.Mutex
|
||||
calls := map[string]int{}
|
||||
return &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
if req.URL.Host != "offline.invalid" {
|
||||
return nil, fmt.Errorf("unexpected offline target")
|
||||
}
|
||||
var body struct {
|
||||
Model string `json:"model"`
|
||||
}
|
||||
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mu.Lock()
|
||||
calls[body.Model]++
|
||||
n := calls[body.Model]
|
||||
mu.Unlock()
|
||||
if n == 1 {
|
||||
return providerResponse(200, "not JSON", nil), nil
|
||||
}
|
||||
switch body.Model {
|
||||
case "synthetic-grammar":
|
||||
return providerResponse(200, validProposal, nil), nil
|
||||
case "synthetic-review":
|
||||
return providerResponse(200, validAssessment, nil), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected synthetic model")
|
||||
}
|
||||
})}
|
||||
}
|
||||
Reference in New Issue
Block a user