Skip to content

Commit

Permalink
anthropic fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
theprimeagen authored and ThePrimeagen committed Aug 20, 2024
1 parent 0a58575 commit 09078b5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
23 changes: 10 additions & 13 deletions examples/v2/td/ai/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type StatefulOpenAIChat struct {
ctx context.Context
}

func (s *StatefulOpenAIChat) Name() string {
return "openai"
}

func NewStatefulOpenAIChat(system string, ctx context.Context) *StatefulOpenAIChat {
return &StatefulOpenAIChat{
ai: NewOpenAIChat(os.Getenv("OPENAI_API_KEY"), system),
Expand Down Expand Up @@ -100,7 +104,7 @@ func NewClaudeSonnet(system string, ctx context.Context) *ClaudeSonnet {
return &ClaudeSonnet{
ctx: ctx,
client: client,
system: system,
system: system + "\n" + shapeMessage,
}
}

Expand All @@ -110,19 +114,8 @@ func (o *ClaudeSonnet) chat(chat string, ctx context.Context) (string, error) {
Model: anthropic.ModelClaude3Sonnet20240229,
Temperature: &temperature,
MaxTokens: 1000,
System: o.system,
Messages: []anthropic.Message{
anthropic.Message{
Role: "system",
Content: []anthropic.MessageContent{
anthropic.NewTextMessageContent(o.system),
},
},
anthropic.Message{
Role: anthropic.RoleUser,
Content: []anthropic.MessageContent{
anthropic.NewTextMessageContent(chat),
},
},
anthropic.Message{
Role: anthropic.RoleUser,
Content: []anthropic.MessageContent{
Expand All @@ -139,6 +132,10 @@ func (o *ClaudeSonnet) chat(chat string, ctx context.Context) (string, error) {
return resp.Content[0].GetText(), nil
}

func (s *ClaudeSonnet) Name() string {
return "anthropic"
}

func (s *ClaudeSonnet) ReadWithTimeout(p string, t time.Duration) (string, error) {
ctx, cancel := context.WithCancel(s.ctx)
go func() {
Expand Down
5 changes: 5 additions & 0 deletions examples/v2/td/players/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type AIResponder struct {

type AIFetcher interface {
ReadWithTimeout(prompt string, t time.Duration) (string, error)
Name() string
}

func NewFetchPosition(ai AIFetcher, debug *testies.DebugFile) AIResponder {
Expand Down Expand Up @@ -144,6 +145,10 @@ func (f *AIResponder) fetchResults(team uint8, gs *objects.GameState, ctx contex
return responses
}

func (f *AIResponder) Name() string {
return f.ai.Name()
}

func (f *AIResponder) StreamResults(team uint8, gs *objects.GameState, out PositionChan, done Done, ctx context.Context) {
if !f.streamResults {
return
Expand Down
2 changes: 2 additions & 0 deletions examples/v2/td/players/box-strat.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ func (r *RandomPos) Stats() objects.Stats {
return objects.Stats{}
}

func (f *RandomPos) Name() string { return "random-pos" }
func (f *RandomPos) Run(ctx context.Context) { }
func (f *BoxPos) Run(ctx context.Context) { }
func (f *BoxPos) Name() string { return "box-pos" }

func StratPlayerFromString(arg string) Player {
assert.Assert(strings.HasPrefix(arg, "strat"), "invalid player string for strat client", "arg", arg)
Expand Down
1 change: 1 addition & 0 deletions examples/v2/td/players/players.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Player interface {
StreamResults(team uint8, gs *objects.GameState, out PositionChan, done Done, ctx context.Context)
Stats() objects.Stats
Run(ctx context.Context)
Name() string
}

type TeamPlayer struct {
Expand Down
4 changes: 4 additions & 0 deletions examples/v2/td/players/twitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (t *TwitchTDChat) Moves(gs *objects.GameState, ctx context.Context) []objec
return occurrencesToPositions(occs, gs.AllowedTowers)
}

func (r *TwitchTDChat) Name() string {
return "twitch-" + r.chat
}

func (r *TwitchTDChat) runStreamResults(gs *objects.GameState, out PositionChan, _ Done, ctx context.Context) {
outer:
for {
Expand Down

0 comments on commit 09078b5

Please sign in to comment.