Skip to content

Commit

Permalink
chore: give an indentity to the assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
henomis committed Feb 24, 2024
1 parent 598dcf0 commit aadf25d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
46 changes: 38 additions & 8 deletions assistant/assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ package assistant

import (
"context"
"strings"

"github.com/henomis/lingoose/thread"
"github.com/henomis/lingoose/types"
)

type Parameters struct {
AssistantName string
AssistantIdentity string
AssistantScope string
CompanyName string
CompanyDescription string
}

type Assistant struct {
llm LLM
rag RAG
thread *thread.Thread
llm LLM
rag RAG
thread *thread.Thread
parameters Parameters
}

type LLM interface {
Expand All @@ -26,6 +34,13 @@ func New(llm LLM) *Assistant {
assistant := &Assistant{
llm: llm,
thread: thread.New(),
parameters: Parameters{
AssistantName: defaultAssistantName,
AssistantIdentity: defaultAssistantIdentity,
AssistantScope: defaultAssistantScope,
CompanyName: defaultCompanyName,
CompanyDescription: defaultCompanyDescription,
},
}

return assistant
Expand All @@ -41,6 +56,11 @@ func (a *Assistant) WithRAG(rag RAG) *Assistant {
return a
}

func (a *Assistant) WithParameters(parameters Parameters) *Assistant {
a.parameters = parameters
return a
}

func (a *Assistant) Run(ctx context.Context) error {
if a.thread == nil {
return nil
Expand Down Expand Up @@ -85,15 +105,25 @@ func (a *Assistant) generateRAGMessage(ctx context.Context) error {
return err
}

context := strings.Join(searchResults, "\n\n")

a.thread.AddMessage(thread.NewUserMessage().AddContent(
a.thread.AddMessage(thread.NewSystemMessage().AddContent(
thread.NewTextContent(
systemRAGPrompt,
).Format(
types.M{
"assistantName": a.parameters.AssistantName,
"assistantIdentity": a.parameters.AssistantIdentity,
"assistantScope": a.parameters.AssistantScope,
"companyName": a.parameters.CompanyName,
"companyDescription": a.parameters.CompanyDescription,
},
),
)).AddMessage(thread.NewUserMessage().AddContent(
thread.NewTextContent(
baseRAGPrompt,
).Format(
types.M{
"question": query,
"context": context,
"results": searchResults,
},
),
))
Expand Down
12 changes: 9 additions & 3 deletions assistant/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package assistant

const (
//nolint:lll
baseRAGPrompt = `You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.
Question: {{.question}}
Context: {{.context}}`
baseRAGPrompt = "Use the following pieces of retrieved context to answer the question.\n\nQuestion: {{.question}}\nContext:\n{{range .results}}{{.}}\n\n{{end}}"
//nolint:lll
systemRAGPrompt = "You name is {{.assistantName}}, and you are {{.assistantIdentity}} at {{.companyName}} {{if ne .companyDescription \"\" }}{{.companyDescription}}{{end}}. Your task is to assist humans {{.assistantScope}}."

defaultAssistantName = "AI assistant"
defaultAssistantIdentity = "a helpful and polite assistant"
defaultAssistantScope = "with their questions"
defaultCompanyName = "Lingoose"
defaultCompanyDescription = ""
)
11 changes: 9 additions & 2 deletions examples/assistant/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import (
// download https://raw.githubusercontent.com/hwchase17/chat-your-data/master/state_of_the_union.txt

func main() {
r := rag.NewFusion(
r := rag.New(
index.New(
jsondb.New().WithPersist("db.json"),
openaiembedder.New(openaiembedder.AdaEmbeddingV2),
),
openai.New().WithTemperature(0),
).WithTopK(3)

_, err := os.Stat("db.json")
Expand All @@ -35,6 +34,14 @@ func main() {

a := assistant.New(
openai.New().WithTemperature(0),
).WithParameters(
assistant.Parameters{
AssistantName: "AI Pirate Assistant",
AssistantIdentity: "a pirate and helpful assistant",
AssistantScope: "with their questions replying as a pirate",
CompanyName: "Lingoose",
CompanyDescription: "a pirate company that provides AI assistants to help humans with their questions",
},
).WithRAG(r).WithThread(
thread.New().AddMessages(
thread.NewUserMessage().AddContent(
Expand Down
21 changes: 0 additions & 21 deletions llm/cohere/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,3 @@ func threadToChatMessages(t *thread.Thread) (string, []model.ChatMessage) {

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
// }

0 comments on commit aadf25d

Please sign in to comment.