Skip to content

Commit

Permalink
Feat implement openai functions handler (#90)
Browse files Browse the repository at this point in the history
* chore: implement openai functions handler

* chore: removed openai enabled option

* chore: remove max iteration option

* chore: use a simpler example

* fix

* fix

* remove unused method

* fix
  • Loading branch information
henomis committed Jun 26, 2023
1 parent abfe5ac commit 0d492d4
Show file tree
Hide file tree
Showing 7 changed files with 417 additions and 24 deletions.
1 change: 1 addition & 0 deletions chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
MessageTypeSystem MessageType = "system"
MessageTypeUser MessageType = "user"
MessageTypeAssistant MessageType = "assistant"
MessageTypeFunction MessageType = "function"
)

type PromptMessage struct {
Expand Down
86 changes: 86 additions & 0 deletions examples/chat/functions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"bufio"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/henomis/lingoose/chat"
"github.com/henomis/lingoose/llm/openai"
"github.com/henomis/lingoose/prompt"
)

func main() {
fmt.Printf("What's your name?\n> ")
reader := bufio.NewReader(os.Stdin)
name, _ := reader.ReadString('\n')

chat := chat.New(
chat.PromptMessage{
Type: chat.MessageTypeSystem,
Prompt: prompt.New("You are an helpfull assistant. Reply using percentages not float numbers."),
},
chat.PromptMessage{
Type: chat.MessageTypeUser,
Prompt: prompt.New(
fmt.Sprintf("My name is %s, can you guess my nationality?", name),
),
},
)

llmOpenAI := openai.New(openai.GPT3Dot5Turbo0613, openai.DefaultOpenAITemperature, openai.DefaultOpenAIMaxTokens, true)

llmOpenAI.BindFunction(
GetNationalitiesForName,
"GetNationalitiesForName",
"Use this function to get the nationalities for a given name.",
)

response, err := llmOpenAI.Chat(context.Background(), chat)
if err != nil {
panic(err)
}

fmt.Printf("\n%s", response)

}

type Query struct {
Name string `json:"name" jsonschema:"description=The name to get the nationalities for"`
}

type Country struct {
CountryID string `json:"country_id"`
Probability float64 `json:"probability"`
}

type NationalizeResponse struct {
Name string `json:"name"`
Country []Country `json:"country"`
}

func GetNationalitiesForName(query Query) ([]Country, error) {
url := fmt.Sprintf("https://api.nationalize.io/?name=%s", query.Name)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var response NationalizeResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}

return response.Country, nil
}
File renamed without changes.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ require (
github.com/google/uuid v1.3.0
github.com/henomis/pinecone-go v1.1.1
github.com/henomis/qdrant-go v1.0.0
github.com/invopop/jsonschema v0.7.0
github.com/pkoukk/tiktoken-go v0.1.1
github.com/sashabaranov/go-openai v1.11.1
github.com/sashabaranov/go-openai v1.12.0
)

require (
github.com/dlclark/regexp2 v1.8.1 // indirect
github.com/henomis/restclientgo v1.0.3 // indirect
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
)
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/dlclark/regexp2 v1.8.1 h1:6Lcdwya6GjPUNsBct8Lg/yRPwMhABj269AAzdGSiR+0=
github.com/dlclark/regexp2 v1.8.1/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
Expand All @@ -9,12 +10,19 @@ github.com/henomis/qdrant-go v1.0.0 h1:KVd9aTvObVJgQFznM0FPMn3+zC4O1ekXgRcG61bW1
github.com/henomis/qdrant-go v1.0.0/go.mod h1:CJ+imAe+WK3ntoIn7v7sSqimGu+/In/7ijhhT0MC5WU=
github.com/henomis/restclientgo v1.0.3 h1:y5+ydfvWJ0/7crObdnCHSn7ya/h1whD+PV4Ir2dZ9Ig=
github.com/henomis/restclientgo v1.0.3/go.mod h1:xIeTCu2ZstvRn0fCukNpzXLN3m/kRTU0i0RwAbv7Zug=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy770So=
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pkoukk/tiktoken-go v0.1.1 h1:jtkYlIECjyM9OW1w4rjPmTohK4arORP9V25y6TM6nXo=
github.com/pkoukk/tiktoken-go v0.1.1/go.mod h1:boMWvk9pQCOTx11pgu0DrIdrAKgQzzJKUP6vLXaz7Rw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/sashabaranov/go-openai v1.11.1 h1:QAyLx/MhiVrV4nsGTAZI+JPP/n0nlYZfxltzv9jTClw=
github.com/sashabaranov/go-openai v1.11.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sashabaranov/go-openai v1.12.0 h1:aRNHH0gtVfrpIaEolD0sWrLLRnYQNK4cH/bIAHwL8Rk=
github.com/sashabaranov/go-openai v1.12.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Loading

0 comments on commit 0d492d4

Please sign in to comment.