Refactor entire system to be more contextual so that conversation flow can be more easily managed
This commit is contained in:
97
context.go
Normal file
97
context.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package go_llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
context.Context
|
||||
request Request
|
||||
response *ResponseChoice
|
||||
toolcall *ToolCall
|
||||
}
|
||||
|
||||
func (c *Context) ToNewRequest(toolResults ...ToolCallResponse) Request {
|
||||
var res Request
|
||||
|
||||
res.Toolbox = c.request.Toolbox
|
||||
res.Temperature = c.request.Temperature
|
||||
|
||||
res.Conversation = make([]Input, len(c.request.Conversation))
|
||||
copy(res.Conversation, c.request.Conversation)
|
||||
|
||||
// now for every input message, convert those to an Input to add to the conversation
|
||||
for _, msg := range c.request.Messages {
|
||||
res.Conversation = append(res.Conversation, msg)
|
||||
}
|
||||
|
||||
// if there are tool calls, then we need to add those to the conversation
|
||||
if c.response != nil {
|
||||
for _, call := range c.response.Calls {
|
||||
res.Conversation = append(res.Conversation, call)
|
||||
|
||||
if c.response.Content != "" || c.response.Refusal != "" {
|
||||
res.Conversation = append(res.Conversation, Message{
|
||||
Role: RoleAssistant,
|
||||
Text: c.response.Content,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if there are tool results, then we need to add those to the conversation
|
||||
for _, result := range toolResults {
|
||||
res.Conversation = append(res.Conversation, result)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func NewContext(ctx context.Context, request Request, response *ResponseChoice, toolcall *ToolCall) *Context {
|
||||
return &Context{Context: ctx, request: request, response: response, toolcall: toolcall}
|
||||
}
|
||||
|
||||
func (c *Context) Request() Request {
|
||||
return c.request
|
||||
}
|
||||
|
||||
func (c *Context) WithContext(ctx context.Context) *Context {
|
||||
return &Context{Context: ctx, request: c.request, response: c.response, toolcall: c.toolcall}
|
||||
}
|
||||
|
||||
func (c *Context) WithRequest(request Request) *Context {
|
||||
return &Context{Context: c.Context, request: request, response: c.response, toolcall: c.toolcall}
|
||||
}
|
||||
|
||||
func (c *Context) WithResponse(response *ResponseChoice) *Context {
|
||||
return &Context{Context: c.Context, request: c.request, response: response, toolcall: c.toolcall}
|
||||
}
|
||||
|
||||
func (c *Context) WithToolCall(toolcall *ToolCall) *Context {
|
||||
return &Context{Context: c.Context, request: c.request, response: c.response, toolcall: toolcall}
|
||||
}
|
||||
|
||||
func (c *Context) Deadline() (deadline time.Time, ok bool) {
|
||||
return c.Context.Deadline()
|
||||
}
|
||||
|
||||
func (c *Context) Done() <-chan struct{} {
|
||||
return c.Context.Done()
|
||||
}
|
||||
|
||||
func (c *Context) Err() error {
|
||||
return c.Context.Err()
|
||||
}
|
||||
|
||||
func (c *Context) Value(key any) any {
|
||||
if key == "request" {
|
||||
return c.request
|
||||
}
|
||||
return c.Context.Value(key)
|
||||
}
|
||||
|
||||
func (c *Context) WithTimeout(timeout time.Duration) (*Context, context.CancelFunc) {
|
||||
ctx, cancel := context.WithTimeout(c.Context, timeout)
|
||||
return c.WithContext(ctx), cancel
|
||||
}
|
Reference in New Issue
Block a user