-
Notifications
You must be signed in to change notification settings - Fork 7
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
13 changed files
with
735 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"go.testFlags": ["-v"] | ||
} |
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,155 @@ | ||
package chain | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hupe1980/golc" | ||
"github.com/hupe1980/golc/integration/sqldb" | ||
"github.com/hupe1980/golc/prompt" | ||
"github.com/hupe1980/golc/schema" | ||
) | ||
|
||
const defaultSQLTemplate = `Given an input question, first create a syntactically correct {{.dialect}} query to run, then look at the results of the query and return the answer. Unless the user specifies in his question a specific number of examples he wishes to obtain, always limit your query to at most {{.topK}} results. You can order the results by a relevant column to return the most interesting examples in the database. | ||
Never query for all the columns from a specific table, only ask for a the few relevant columns given the question. | ||
Pay attention to use only the column names that you can see in the schema description. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table. | ||
Use the following format: | ||
Question: Question here | ||
SQLQuery: SQL Query to run | ||
SQLResult: Result of the SQLQuery | ||
Answer: Final answer here | ||
Only use the following tables: | ||
{{.tableInfo}} | ||
Question: {{.input}}` | ||
|
||
// Compile time check to ensure SQL satisfies the Chain interface. | ||
var _ schema.Chain = (*SQL)(nil) | ||
|
||
type SQLOptions struct { | ||
*schema.CallbackOptions | ||
InputKey string | ||
TablesInputKey string | ||
OutputKey string | ||
TopK uint | ||
} | ||
|
||
type SQL struct { | ||
sqldb *sqldb.SQLDB | ||
llmChain *LLM | ||
opts SQLOptions | ||
} | ||
|
||
func NewSQL(llm schema.Model, engine sqldb.Engine) (*SQL, error) { | ||
opts := SQLOptions{ | ||
InputKey: "query", | ||
OutputKey: "result", | ||
TopK: 5, | ||
CallbackOptions: &schema.CallbackOptions{ | ||
Verbose: golc.Verbose, | ||
}, | ||
} | ||
|
||
sqldb, err := sqldb.New(engine) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
llmChain, err := NewLLM(llm, prompt.NewTemplate(defaultSQLTemplate)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SQL{ | ||
sqldb: sqldb, | ||
llmChain: llmChain, | ||
opts: opts, | ||
}, nil | ||
} | ||
|
||
// Call executes the SQL chain with the given context and inputs. | ||
// It returns the outputs of the chain or an error, if any. | ||
func (c *SQL) Call(ctx context.Context, inputs schema.ChainValues, optFns ...func(o *schema.CallOptions)) (schema.ChainValues, error) { | ||
query, ok := inputs[c.opts.InputKey].(string) | ||
if !ok { | ||
return nil, fmt.Errorf("%w: no value for inputKey %s", ErrInvalidInputValues, c.opts.InputKey) | ||
} | ||
|
||
tableInfo, err := c.sqldb.TableInfo(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
input := fmt.Sprintf("%s\nSQLQuery:", query) | ||
|
||
sqlQuery, err := golc.SimpleCall(ctx, c.llmChain, schema.ChainValues{ | ||
"dialect": c.sqldb.Dialect(), | ||
"input": input, | ||
"tableInfo": tableInfo, | ||
"topK": c.opts.TopK, | ||
}, func(sco *golc.SimpleCallOptions) { | ||
sco.Stop = []string{"\nSQLResult:"} | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
queryResult, err := c.sqldb.Query(ctx, sqlQuery) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
input += fmt.Sprintf("%s\nSQLResult: %s\nAnswer:", sqlQuery, queryResult) | ||
|
||
result, err := golc.SimpleCall(ctx, c.llmChain, schema.ChainValues{ | ||
"dialect": c.sqldb.Dialect(), | ||
"input": input, | ||
"tableInfo": tableInfo, | ||
"topK": c.opts.TopK, | ||
}, func(sco *golc.SimpleCallOptions) { | ||
sco.Stop = []string{"\nSQLResult:"} | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return schema.ChainValues{ | ||
c.opts.OutputKey: strings.TrimSpace(result), | ||
}, nil | ||
} | ||
|
||
// Memory returns the memory associated with the chain. | ||
func (c *SQL) Memory() schema.Memory { | ||
return nil | ||
} | ||
|
||
// Type returns the type of the chain. | ||
func (c *SQL) Type() string { | ||
return "SQL" | ||
} | ||
|
||
// Verbose returns the verbosity setting of the chain. | ||
func (c *SQL) Verbose() bool { | ||
return c.opts.CallbackOptions.Verbose | ||
} | ||
|
||
// Callbacks returns the callbacks associated with the chain. | ||
func (c *SQL) Callbacks() []schema.Callback { | ||
return c.opts.CallbackOptions.Callbacks | ||
} | ||
|
||
// InputKeys returns the expected input keys. | ||
func (c *SQL) InputKeys() []string { | ||
return []string{c.opts.InputKey} | ||
} | ||
|
||
// OutputKeys returns the output keys the chain will return. | ||
func (c *SQL) OutputKeys() []string { | ||
return []string{c.opts.OutputKey} | ||
} |
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,67 @@ | ||
--- | ||
title: SQL | ||
description: All about sql chains. | ||
weight: 70 | ||
--- | ||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/hupe1980/golc" | ||
"github.com/hupe1980/golc/chain" | ||
"github.com/hupe1980/golc/integration/sqldb" | ||
"github.com/hupe1980/golc/model/llm" | ||
|
||
// Add your sql db driver, see https://github.com/golang/go/wiki/SQLDrivers | ||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
openai, err := llm.NewOpenAI(os.Getenv("OPENAI_API_KEY")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
engine, err := sqldb.NewSQLite3(":memory:") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Only for demonstration | ||
_, exErr := engine.Exec(ctx, "CREATE TABLE IF NOT EXISTS employee ( id int not null );") | ||
if exErr != nil { | ||
log.Fatal(exErr) | ||
} | ||
|
||
// Only for demonstration | ||
for i := 0; i < 4; i++ { | ||
_, qErr := engine.Exec(ctx, "INSERT INTO employee (id) VALUES (?) ;", i) | ||
if qErr != nil { | ||
log.Fatal(qErr) | ||
} | ||
} | ||
|
||
sql, err := chain.NewSQL(openai, engine) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
result, err := golc.SimpleCall(ctx, sql, "How many employees are there?") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(result) | ||
} | ||
``` | ||
Output: | ||
```text | ||
There are 4 employees. | ||
``` |
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,53 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/hupe1980/golc" | ||
"github.com/hupe1980/golc/chain" | ||
"github.com/hupe1980/golc/integration/sqldb" | ||
"github.com/hupe1980/golc/model/llm" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
openai, err := llm.NewOpenAI(os.Getenv("OPENAI_API_KEY")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
engine, err := sqldb.NewSQLite3(":memory:") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
_, exErr := engine.Exec(ctx, "CREATE TABLE IF NOT EXISTS employee ( id int not null );") | ||
if exErr != nil { | ||
log.Fatal(exErr) | ||
} | ||
|
||
for i := 0; i < 4; i++ { | ||
_, qErr := engine.Exec(ctx, "INSERT INTO employee (id) VALUES (?) ;", i) | ||
if qErr != nil { | ||
log.Fatal(qErr) | ||
} | ||
} | ||
|
||
sql, err := chain.NewSQL(openai, engine) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
result, err := golc.SimpleCall(ctx, sql, "How many employees are there?") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(result) | ||
} |
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
Oops, something went wrong.