-
Notifications
You must be signed in to change notification settings - Fork 60
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
10 changed files
with
230 additions
and
3 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
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,40 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
openaiembedder "github.com/henomis/lingoose/embedder/openai" | ||
"github.com/henomis/lingoose/index" | ||
"github.com/henomis/lingoose/index/vectordb/jsondb" | ||
"github.com/henomis/lingoose/linglet/qa" | ||
"github.com/henomis/lingoose/llm/openai" | ||
) | ||
|
||
// download https://raw.githubusercontent.com/hwchase17/chat-your-data/master/state_of_the_union.txt | ||
|
||
func main() { | ||
qa := qa.New( | ||
openai.New().WithTemperature(0), | ||
index.New( | ||
jsondb.New().WithPersist("db.json"), | ||
openaiembedder.New(openaiembedder.AdaEmbeddingV2), | ||
), | ||
) | ||
|
||
_, err := os.Stat("db.json") | ||
if os.IsNotExist(err) { | ||
err = qa.AddSource(context.Background(), "state_of_the_union.txt") | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
response, err := qa.Run(context.Background(), "What is the NATO purpose?") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(response) | ||
} |
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,24 @@ | ||
package qa | ||
|
||
//nolint:lll | ||
const ( | ||
refinementPrompt = ` | ||
You are a Prompt Engineer with 10 years of experience. Given a prompt, refine the prompt to reflect these tatics, include details in your query to get more relevant answers. | ||
The refined prompt should: | ||
1. Include details in prompt to get more relevant answers. | ||
Example: | ||
How do I add numbers in Excel? -> How do I add up a row of dollar amounts in Excel? I want to do this automatically for a whole sheet of rows with all the totals ending up on the right in a column called "Total". | ||
2. Ask the model to adopt a role | ||
Example: | ||
How do I add numbers in Excel? -> You are an Excel expert with 10 years experience. ... | ||
3. Specify the steps required to complete a task. Some tasks are best specified as a sequence of steps. Writing the steps out explicitly can make it easier for the model to follow them. | ||
4. Provide examples | ||
The prompt is : {{.prompt}} | ||
Only return the refined prompt as output. Merge the final prompt into one sentence or paragraph.` | ||
) |
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,94 @@ | ||
package qa | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/henomis/lingoose/assistant" | ||
"github.com/henomis/lingoose/index" | ||
"github.com/henomis/lingoose/rag" | ||
"github.com/henomis/lingoose/thread" | ||
"github.com/henomis/lingoose/types" | ||
) | ||
|
||
const ( | ||
defaultTopK = 3 | ||
) | ||
|
||
type LLM interface { | ||
Generate(context.Context, *thread.Thread) error | ||
} | ||
|
||
type QA struct { | ||
llm LLM | ||
index *index.Index | ||
subDocumentRAG *rag.SubDocumentRAG | ||
} | ||
|
||
func New( | ||
llm LLM, | ||
index *index.Index, | ||
) *QA { | ||
subDocumentRAG := rag.NewSubDocument(index, llm).WithTopK(defaultTopK) | ||
|
||
return &QA{ | ||
llm: llm, | ||
index: index, | ||
subDocumentRAG: subDocumentRAG, | ||
} | ||
} | ||
|
||
func (qa *QA) refinePrompt(ctx context.Context, prompt string) (string, error) { | ||
t := thread.New().AddMessage( | ||
thread.NewAssistantMessage().AddContent( | ||
thread.NewTextContent(refinementPrompt). | ||
Format( | ||
types.M{ | ||
"prompt": prompt, | ||
}, | ||
), | ||
), | ||
) | ||
|
||
err := qa.llm.Generate(ctx, t) | ||
if err != nil { | ||
return prompt, err | ||
} | ||
|
||
return t.LastMessage().Contents[0].AsString(), nil | ||
} | ||
|
||
func (qa *QA) AddSource(ctx context.Context, source string) error { | ||
return qa.subDocumentRAG.AddSources(ctx, source) | ||
} | ||
|
||
func (qa *QA) Run(ctx context.Context, prompt string) (string, error) { | ||
refinedPromt, err := qa.refinePrompt(ctx, prompt) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
a := assistant.New( | ||
qa.llm, | ||
).WithParameters( | ||
assistant.Parameters{ | ||
AssistantName: "AI Assistant", | ||
AssistantIdentity: "a helpful and polite assistant", | ||
AssistantScope: "to answer questions", | ||
CompanyName: "", | ||
CompanyDescription: "", | ||
}, | ||
).WithRAG(qa.subDocumentRAG).WithThread( | ||
thread.New().AddMessages( | ||
thread.NewUserMessage().AddContent( | ||
thread.NewTextContent(refinedPromt), | ||
), | ||
), | ||
) | ||
|
||
err = a.Run(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return a.Thread().LastMessage().Contents[0].AsString(), nil | ||
} |
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