Initial import
This commit is contained in:
13
agent/agent_suite_test.go
Normal file
13
agent/agent_suite_test.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package agent_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestAgent(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Agent test suite")
|
||||
}
|
||||
101
agent/constructor.go
Normal file
101
agent/constructor.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"github.com/mudler/local-agent-framework/llm"
|
||||
"github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
type llmOptions struct {
|
||||
APIURL string
|
||||
Model string
|
||||
}
|
||||
|
||||
type options struct {
|
||||
LLMAPI llmOptions
|
||||
Character Character
|
||||
}
|
||||
|
||||
type Agent struct {
|
||||
options *options
|
||||
Character Character
|
||||
client *openai.Client
|
||||
}
|
||||
|
||||
type Option func(*options) error
|
||||
|
||||
func defaultOptions() *options {
|
||||
return &options{
|
||||
LLMAPI: llmOptions{
|
||||
APIURL: "http://localhost:8080",
|
||||
Model: "echidna",
|
||||
},
|
||||
Character: Character{
|
||||
Name: "John Doe",
|
||||
Age: 0,
|
||||
Occupation: "Unemployed",
|
||||
NowDoing: "Nothing",
|
||||
DoingNext: "Nothing",
|
||||
DoneHistory: []string{},
|
||||
Memories: []string{},
|
||||
Hobbies: []string{},
|
||||
MusicTaste: []string{},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newOptions(opts ...Option) (*options, error) {
|
||||
options := defaultOptions()
|
||||
for _, o := range opts {
|
||||
if err := o(options); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return options, nil
|
||||
}
|
||||
|
||||
func New(opts ...Option) (*Agent, error) {
|
||||
options, err := newOptions(opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := llm.NewClient("", options.LLMAPI.APIURL)
|
||||
a := &Agent{
|
||||
options: options,
|
||||
client: client,
|
||||
Character: options.Character,
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func WithLLMAPIURL(url string) Option {
|
||||
return func(o *options) error {
|
||||
o.LLMAPI.APIURL = url
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithModel(model string) Option {
|
||||
return func(o *options) error {
|
||||
o.LLMAPI.Model = model
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithCharacter(c Character) Option {
|
||||
return func(o *options) error {
|
||||
o.Character = c
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func FromFile(path string) Option {
|
||||
return func(o *options) error {
|
||||
c, err := Load(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o.Character = *c
|
||||
return nil
|
||||
}
|
||||
}
|
||||
48
agent/state.go
Normal file
48
agent/state.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"github.com/mudler/local-agent-framework/llm"
|
||||
)
|
||||
|
||||
type Character struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
Occupation string `json:"job_occupation"`
|
||||
NowDoing string `json:"doing_now"`
|
||||
DoingNext string `json:"doing_next"`
|
||||
DoneHistory []string `json:"done_history"`
|
||||
Memories []string `json:"memories"`
|
||||
Hobbies []string `json:"hobbies"`
|
||||
MusicTaste []string `json:"music_taste"`
|
||||
}
|
||||
|
||||
func Load(path string) (*Character, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var c Character
|
||||
err = json.Unmarshal(data, &c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func (a *Agent) Save(path string) error {
|
||||
data, err := json.Marshal(a.options.Character)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, 0644)
|
||||
}
|
||||
|
||||
func (a *Agent) GenerateIdentity(guidance string) error {
|
||||
err := llm.GenerateJSONFromStruct(a.client, guidance, a.options.LLMAPI.Model, &a.options.Character)
|
||||
|
||||
a.Character = a.options.Character
|
||||
return err
|
||||
}
|
||||
34
agent/state_test.go
Normal file
34
agent/state_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package agent_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
. "github.com/mudler/local-agent-framework/agent"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Agent test", func() {
|
||||
|
||||
Context("identity", func() {
|
||||
|
||||
It("generates all the fields", func() {
|
||||
agent, err := New(
|
||||
WithLLMAPIURL("http://192.168.68.113:8080"),
|
||||
WithModel("echidna"))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
err = agent.GenerateIdentity("An old man with a long beard, a wizard, who lives in a tower.")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(agent.Character.Name).ToNot(BeEmpty())
|
||||
Expect(agent.Character.Age).ToNot(BeZero())
|
||||
Expect(agent.Character.Occupation).ToNot(BeEmpty())
|
||||
Expect(agent.Character.NowDoing).ToNot(BeEmpty())
|
||||
Expect(agent.Character.DoingNext).ToNot(BeEmpty())
|
||||
Expect(agent.Character.DoneHistory).ToNot(BeEmpty())
|
||||
Expect(agent.Character.Memories).ToNot(BeEmpty())
|
||||
Expect(agent.Character.Hobbies).ToNot(BeEmpty())
|
||||
Expect(agent.Character.MusicTaste).ToNot(BeEmpty())
|
||||
fmt.Printf("%+v\n", agent.Character)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user