-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
206 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/henomis/lingoose/llm/cohere" | ||
"github.com/henomis/lingoose/thread" | ||
) | ||
|
||
func main() { | ||
t := thread.New() | ||
t.AddMessage(thread.NewUserMessage().AddContent( | ||
thread.NewTextContent("Hello"), | ||
)) | ||
|
||
err := cohere.New().WithMaxTokens(1000).WithTemperature(0). | ||
WithStream( | ||
func(s string) { | ||
fmt.Print(s) | ||
}, | ||
).Generate(context.Background(), t) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(t.String()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cohere | ||
|
||
import ( | ||
"github.com/henomis/cohere-go/model" | ||
"github.com/henomis/cohere-go/request" | ||
"github.com/henomis/lingoose/thread" | ||
) | ||
|
||
var threadRoleToCohereRole = map[thread.Role]model.ChatMessageRole{ | ||
thread.RoleSystem: model.ChatMessageRoleChatbot, | ||
thread.RoleUser: model.ChatMessageRoleUser, | ||
thread.RoleAssistant: model.ChatMessageRoleChatbot, | ||
} | ||
|
||
func (c *Cohere) buildChatCompletionRequest(t *thread.Thread) *request.Chat { | ||
message, history := threadToChatMessages(t) | ||
|
||
return &request.Chat{ | ||
Model: c.model, | ||
ChatHistory: history, | ||
Message: message, | ||
} | ||
} | ||
|
||
func threadToChatMessages(t *thread.Thread) (string, []model.ChatMessage) { | ||
var history []model.ChatMessage | ||
var message string | ||
|
||
for _, m := range t.Messages { | ||
chatMessage := model.ChatMessage{ | ||
Role: threadRoleToCohereRole[m.Role], | ||
} | ||
|
||
switch m.Role { | ||
case thread.RoleUser, thread.RoleSystem, thread.RoleAssistant: | ||
for _, content := range m.Contents { | ||
if content.Type == thread.ContentTypeText { | ||
chatMessage.Message += content.Data.(string) + "\n" | ||
} | ||
} | ||
case thread.RoleTool: | ||
continue | ||
} | ||
|
||
history = append(history, chatMessage) | ||
} | ||
|
||
lastMessage := t.LastMessage() | ||
if lastMessage.Role == thread.RoleUser { | ||
for _, content := range lastMessage.Contents { | ||
if content.Type == thread.ContentTypeText { | ||
message += content.Data.(string) + "\n" | ||
} | ||
} | ||
|
||
history = history[:len(history)-1] | ||
} | ||
|
||
return message, history | ||
} | ||
|
||
// chatMessages := make([]message, len(t.Messages)) | ||
// for i, m := range t.Messages { | ||
// chatMessages[i] = message{ | ||
// Role: threadRoleToOpenAIRole[m.Role], | ||
// } | ||
|
||
// switch m.Role { | ||
// case thread.RoleUser, thread.RoleSystem, thread.RoleAssistant: | ||
// for _, content := range m.Contents { | ||
// if content.Type == thread.ContentTypeText { | ||
// chatMessages[i].Content += content.Data.(string) + "\n" | ||
// } | ||
// } | ||
// case thread.RoleTool: | ||
// continue | ||
// } | ||
// } | ||
|
||
// return chatMessages | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters