mirror of
https://github.com/vxcontrol/langchaingo.git
synced 2026-07-22 09:25:25 -04:00
aee820e3bb
* ollama: Add use of new chat endpoint
63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/tmc/langchaingo/llms/ernie"
|
|
|
|
"github.com/tmc/langchaingo/llms"
|
|
"github.com/tmc/langchaingo/schema"
|
|
)
|
|
|
|
func main() {
|
|
llm, err := ernie.NewChat(
|
|
ernie.WithModelName(ernie.ModelNameERNIEBot),
|
|
// Fill in your AK and SK here.
|
|
ernie.WithAKSK("ak", "sk"),
|
|
// Use an external cache for the access token.
|
|
ernie.WithAccessToken("accesstoken"),
|
|
)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
completion, err := llm.Call(ctx, []schema.ChatMessage{
|
|
schema.HumanChatMessage{Content: "What is the weather like in Boston?"},
|
|
}, llms.WithFunctions(functions))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if completion != nil {
|
|
fmt.Printf("Function call: %v\n", completion.FunctionCall)
|
|
}
|
|
}
|
|
|
|
func getCurrentWeather(location string, unit string) (string, error) {
|
|
weatherInfo := map[string]interface{}{
|
|
"location": location,
|
|
"temperature": "72",
|
|
"unit": unit,
|
|
"forecast": []string{"sunny", "windy"},
|
|
}
|
|
b, err := json.Marshal(weatherInfo)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
var functions = []llms.FunctionDefinition{
|
|
{
|
|
Name: "getCurrentWeather",
|
|
Description: "Get the current weather in a given location",
|
|
Parameters: json.RawMessage(`{"type": "object", "properties": {"location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}}, "required": ["location"]}`),
|
|
},
|
|
}
|