first commit
This commit is contained in:
12
examples/plugins/llm-only/Makefile
Normal file
12
examples/plugins/llm-only/Makefile
Normal file
@@ -0,0 +1,12 @@
|
||||
.PHONY: build clean
|
||||
|
||||
build:
|
||||
@echo "Building LLM-Only plugin..."
|
||||
@mkdir -p build
|
||||
@go build -buildmode=plugin -o build/llm-only.so main.go
|
||||
@echo "Plugin built successfully: build/llm-only.so"
|
||||
|
||||
clean:
|
||||
@echo "Cleaning build directory..."
|
||||
@rm -rf build
|
||||
@echo "Clean complete"
|
||||
103
examples/plugins/llm-only/README.md
Normal file
103
examples/plugins/llm-only/README.md
Normal file
@@ -0,0 +1,103 @@
|
||||
# LLM-Only Plugin Example
|
||||
|
||||
This example demonstrates a plugin that only implements the `LLMPlugin` interface.
|
||||
|
||||
## Features
|
||||
|
||||
- **PreLLMHook**: Intercepts requests before they reach the LLM provider
|
||||
- Logs request details
|
||||
- Modifies requests (adds system message)
|
||||
- Stores metadata in context
|
||||
|
||||
- **PostLLMHook**: Intercepts responses after the LLM provider responds
|
||||
- Logs response details
|
||||
- Accesses context metadata
|
||||
- Handles errors
|
||||
|
||||
## Use Cases
|
||||
|
||||
- Request/response logging
|
||||
- Adding default system messages
|
||||
- Request validation
|
||||
- Response filtering
|
||||
- Token counting
|
||||
- Cost tracking
|
||||
|
||||
## Building
|
||||
|
||||
```bash
|
||||
make build
|
||||
```
|
||||
|
||||
This creates `build/llm-only.so`
|
||||
|
||||
## Configuration
|
||||
|
||||
Add to your Bifrost config:
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": [
|
||||
{
|
||||
"path": "/path/to/llm-only.so",
|
||||
"name": "llm-only",
|
||||
"display_name": "LLM Request Logger",
|
||||
"enabled": true,
|
||||
"type": "llm",
|
||||
"config": {
|
||||
"inject_system_message": true,
|
||||
"system_message_text": "You are a helpful assistant.",
|
||||
"enable_logging": true,
|
||||
"log_requests": true,
|
||||
"log_responses": true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
**Note:**
|
||||
- `name` is the system identifier (from `GetName()`) and is **not editable**
|
||||
- `display_name` is shown in the UI and is **editable** by users
|
||||
|
||||
### Configuration Options
|
||||
|
||||
| Option | Type | Default | Description |
|
||||
|--------|------|---------|-------------|
|
||||
| `inject_system_message` | boolean | `true` | Enable/disable automatic system message injection |
|
||||
| `system_message_text` | string | `"You are a helpful assistant..."` | Custom system message to inject |
|
||||
| `enable_logging` | boolean | `true` | Enable/disable detailed logging |
|
||||
| `log_requests` | boolean | `true` | Log request details (provider, model) |
|
||||
| `log_responses` | boolean | `true` | Log response details (ID, choices) |
|
||||
|
||||
### Example Configurations
|
||||
|
||||
**Minimal logging:**
|
||||
```json
|
||||
{
|
||||
"config": {
|
||||
"enable_logging": false,
|
||||
"log_requests": false,
|
||||
"log_responses": false
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Custom system message:**
|
||||
```json
|
||||
{
|
||||
"config": {
|
||||
"inject_system_message": true,
|
||||
"system_message_text": "You are a technical expert. Provide detailed, accurate answers."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**No system message injection:**
|
||||
```json
|
||||
{
|
||||
"config": {
|
||||
"inject_system_message": false
|
||||
}
|
||||
}
|
||||
```
|
||||
32
examples/plugins/llm-only/go.mod
Normal file
32
examples/plugins/llm-only/go.mod
Normal file
@@ -0,0 +1,32 @@
|
||||
module github.com/maximhq/bifrost/examples/plugins/llm-only
|
||||
|
||||
go 1.26.2
|
||||
|
||||
replace github.com/maximhq/bifrost/core => ../../../core
|
||||
|
||||
require github.com/maximhq/bifrost/core v0.0.0-00010101000000-000000000000
|
||||
|
||||
require (
|
||||
github.com/andybalholm/brotli v1.2.0 // indirect
|
||||
github.com/bahlo/generic-list-go v0.2.0 // indirect
|
||||
github.com/buger/jsonparser v1.1.2 // indirect
|
||||
github.com/bytedance/gopkg v0.1.3 // indirect
|
||||
github.com/bytedance/sonic v1.15.0 // indirect
|
||||
github.com/bytedance/sonic/loader v0.5.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.6 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/invopop/jsonschema v0.13.0 // indirect
|
||||
github.com/klauspost/compress v1.18.2 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||
github.com/mailru/easyjson v0.9.1 // indirect
|
||||
github.com/mark3labs/mcp-go v0.43.2 // indirect
|
||||
github.com/spf13/cast v1.10.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasthttp v1.68.0 // indirect
|
||||
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
|
||||
golang.org/x/arch v0.23.0 // indirect
|
||||
golang.org/x/sys v0.42.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
80
examples/plugins/llm-only/go.sum
Normal file
80
examples/plugins/llm-only/go.sum
Normal file
@@ -0,0 +1,80 @@
|
||||
github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwToPjQ=
|
||||
github.com/andybalholm/brotli v1.2.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
|
||||
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
|
||||
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
|
||||
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
|
||||
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
|
||||
github.com/buger/jsonparser v1.1.2/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
|
||||
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
||||
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
|
||||
github.com/bytedance/sonic v1.14.2 h1:k1twIoe97C1DtYUo+fZQy865IuHia4PR5RPiuGPPIIE=
|
||||
github.com/bytedance/sonic v1.14.2/go.mod h1:T80iDELeHiHKSc0C9tubFygiuXoGzrkjKzX2quAx980=
|
||||
github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k=
|
||||
github.com/bytedance/sonic/loader v0.4.0 h1:olZ7lEqcxtZygCK9EKYKADnpQoYkRQxaeY2NYzevs+o=
|
||||
github.com/bytedance/sonic/loader v0.4.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
|
||||
github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
|
||||
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
|
||||
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/invopop/jsonschema v0.13.0 h1:KvpoAJWEjR3uD9Kbm2HWJmqsEaHt8lBUpd0qHcIi21E=
|
||||
github.com/invopop/jsonschema v0.13.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
|
||||
github.com/klauspost/compress v1.18.2 h1:iiPHWW0YrcFgpBYhsA6D1+fqHssJscY/Tm/y2Uqnapk=
|
||||
github.com/klauspost/compress v1.18.2/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
|
||||
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mailru/easyjson v0.9.1 h1:LbtsOm5WAswyWbvTEOqhypdPeZzHavpZx96/n553mR8=
|
||||
github.com/mailru/easyjson v0.9.1/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
|
||||
github.com/mark3labs/mcp-go v0.43.2 h1:21PUSlWWiSbUPQwXIJ5WKlETixpFpq+WBpbMGDSVy/I=
|
||||
github.com/mark3labs/mcp-go v0.43.2/go.mod h1:YnJfOL382MIWDx1kMY+2zsRHU/q78dBg9aFb8W6Thdw=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
|
||||
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasthttp v1.68.0 h1:v12Nx16iepr8r9ySOwqI+5RBJ/DqTxhOy1HrHoDFnok=
|
||||
github.com/valyala/fasthttp v1.68.0/go.mod h1:5EXiRfYQAoiO/khu4oU9VISC/eVY6JqmSpPJoHCKsz4=
|
||||
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
|
||||
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
|
||||
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
|
||||
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
|
||||
golang.org/x/arch v0.23.0 h1:lKF64A2jF6Zd8L0knGltUnegD62JMFBiCPBmQpToHhg=
|
||||
golang.org/x/arch v0.23.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A=
|
||||
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
142
examples/plugins/llm-only/main.go
Normal file
142
examples/plugins/llm-only/main.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/maximhq/bifrost/core/schemas"
|
||||
)
|
||||
|
||||
// Plugin configuration
|
||||
type PluginConfig struct {
|
||||
InjectSystemMessage bool `json:"inject_system_message"` // Toggle system message injection
|
||||
SystemMessageText string `json:"system_message_text"` // Custom system message
|
||||
EnableLogging bool `json:"enable_logging"` // Toggle detailed logging
|
||||
LogRequests bool `json:"log_requests"` // Log request details
|
||||
LogResponses bool `json:"log_responses"` // Log response details
|
||||
}
|
||||
|
||||
var (
|
||||
// Default configuration
|
||||
pluginConfig = &PluginConfig{
|
||||
InjectSystemMessage: true,
|
||||
SystemMessageText: "You are a helpful assistant. This message was added by an LLM plugin.",
|
||||
EnableLogging: true,
|
||||
LogRequests: true,
|
||||
LogResponses: true,
|
||||
}
|
||||
)
|
||||
|
||||
// Init is called when the plugin is loaded (optional)
|
||||
func Init(config any) error {
|
||||
fmt.Println("[LLM-Only Plugin] Init called")
|
||||
|
||||
// Parse configuration
|
||||
if configMap, ok := config.(map[string]interface{}); ok {
|
||||
if injectMsg, ok := configMap["inject_system_message"].(bool); ok {
|
||||
pluginConfig.InjectSystemMessage = injectMsg
|
||||
fmt.Printf("[LLM-Only Plugin] System message injection: %v\n", pluginConfig.InjectSystemMessage)
|
||||
}
|
||||
|
||||
if msgText, ok := configMap["system_message_text"].(string); ok {
|
||||
pluginConfig.SystemMessageText = msgText
|
||||
fmt.Printf("[LLM-Only Plugin] System message: %s\n", pluginConfig.SystemMessageText)
|
||||
}
|
||||
|
||||
if enableLogging, ok := configMap["enable_logging"].(bool); ok {
|
||||
pluginConfig.EnableLogging = enableLogging
|
||||
fmt.Printf("[LLM-Only Plugin] Logging enabled: %v\n", pluginConfig.EnableLogging)
|
||||
}
|
||||
|
||||
if logReq, ok := configMap["log_requests"].(bool); ok {
|
||||
pluginConfig.LogRequests = logReq
|
||||
}
|
||||
|
||||
if logResp, ok := configMap["log_responses"].(bool); ok {
|
||||
pluginConfig.LogResponses = logResp
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("[LLM-Only Plugin] Configuration loaded: %+v\n", pluginConfig)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetName returns the name of the plugin (required)
|
||||
// This is the system identifier - not editable by users
|
||||
// Users can set a custom display_name in the config for the UI
|
||||
func GetName() string {
|
||||
return "llm-only"
|
||||
}
|
||||
|
||||
// PreLLMHook is called before the LLM provider is invoked
|
||||
// This example demonstrates request modification and logging
|
||||
func PreLLMHook(ctx *schemas.BifrostContext, req *schemas.BifrostRequest) (*schemas.BifrostRequest, *schemas.LLMPluginShortCircuit, error) {
|
||||
if pluginConfig.EnableLogging {
|
||||
fmt.Println("[LLM-Only Plugin] PreLLMHook called")
|
||||
}
|
||||
|
||||
// Example: Log the request (configurable)
|
||||
if pluginConfig.LogRequests && req.ChatRequest != nil {
|
||||
fmt.Printf("[LLM-Only Plugin] Provider: %s, Model: %s\n",
|
||||
req.ChatRequest.Provider, req.ChatRequest.Model)
|
||||
if pluginConfig.EnableLogging {
|
||||
fmt.Printf("[LLM-Only Plugin] Message count: %d\n", len(req.ChatRequest.Input))
|
||||
}
|
||||
}
|
||||
|
||||
// Example: Store metadata in context
|
||||
ctx.SetValue(schemas.BifrostContextKey("llm-plugin-timestamp"), "pre-hook-timestamp")
|
||||
|
||||
// Example: Modify the request (add a system message) - configurable
|
||||
if pluginConfig.InjectSystemMessage && req.ChatRequest != nil && req.ChatRequest.Input != nil {
|
||||
systemMsg := schemas.ChatMessage{
|
||||
Role: "system",
|
||||
Content: &schemas.ChatMessageContent{ContentStr: &pluginConfig.SystemMessageText},
|
||||
}
|
||||
req.ChatRequest.Input = append([]schemas.ChatMessage{systemMsg}, req.ChatRequest.Input...)
|
||||
if pluginConfig.EnableLogging {
|
||||
fmt.Println("[LLM-Only Plugin] System message injected")
|
||||
}
|
||||
}
|
||||
|
||||
// Return modified request, no short-circuit, no error
|
||||
return req, nil, nil
|
||||
}
|
||||
|
||||
// PostLLMHook is called after the LLM provider responds
|
||||
// This example demonstrates response modification and logging
|
||||
func PostLLMHook(ctx *schemas.BifrostContext, resp *schemas.BifrostResponse, bifrostErr *schemas.BifrostError) (*schemas.BifrostResponse, *schemas.BifrostError, error) {
|
||||
if pluginConfig.EnableLogging {
|
||||
fmt.Println("[LLM-Only Plugin] PostLLMHook called")
|
||||
}
|
||||
|
||||
// Retrieve metadata from context
|
||||
if pluginConfig.EnableLogging {
|
||||
timestamp := ctx.Value(schemas.BifrostContextKey("llm-plugin-timestamp"))
|
||||
fmt.Printf("[LLM-Only Plugin] Request timestamp: %v\n", timestamp)
|
||||
}
|
||||
|
||||
// Example: Log the response (configurable)
|
||||
if pluginConfig.LogResponses && resp != nil && resp.ChatResponse != nil {
|
||||
fmt.Printf("[LLM-Only Plugin] Response ID: %s, Model: %s\n",
|
||||
resp.ChatResponse.ID, resp.ChatResponse.Model)
|
||||
if pluginConfig.EnableLogging && len(resp.ChatResponse.Choices) > 0 {
|
||||
fmt.Printf("[LLM-Only Plugin] Choices count: %d\n", len(resp.ChatResponse.Choices))
|
||||
}
|
||||
}
|
||||
|
||||
// Example: Log errors if present
|
||||
if bifrostErr != nil && bifrostErr.Error != nil {
|
||||
fmt.Printf("[LLM-Only Plugin] Error occurred: %v\n", bifrostErr.Error.Message)
|
||||
}
|
||||
|
||||
// Return unmodified response and error
|
||||
return resp, bifrostErr, nil
|
||||
}
|
||||
|
||||
// Cleanup is called when the plugin is unloaded (required)
|
||||
func Cleanup() error {
|
||||
if pluginConfig.EnableLogging {
|
||||
fmt.Println("[LLM-Only Plugin] Cleanup called")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user