Skip to content

Commit

Permalink
examples: Add openai o1-preview example (#1017)
Browse files Browse the repository at this point in the history
examples: add openai-o1-example
  • Loading branch information
tmc authored Sep 13, 2024
1 parent af67ff3 commit f01a52c
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/openai-o1-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# OpenAI Chat Example 🧦🌈

Hello there, sock enthusiast! 👋 Welcome to this fun little Go program that uses OpenAI's chat model to come up with a fantastic company name for a colorful sock business. How exciting!

## What Does This Example Do? 🤔

This nifty program does the following:

1. 🤖 It sets up a chat with an AI that thinks it's a company branding design wizard. How cool is that?

2. 🎨 It asks the AI a very important question: "What would be a good company name for a company that makes colorful socks?"

3. 🖨️ As the AI comes up with ideas, it prints them out in real-time. It's like watching creativity happen before your very eyes!

## How It Works 🛠️

1. First, we create a new OpenAI language model instance.

2. We set up our chat by giving the AI its role (a branding wizard) and asking our burning sock-related question.

3. We then generate content from the AI, setting a max token limit of 1024 (that's plenty for naming socks!).

4. As the AI responds, we print each chunk of its response immediately. It's almost like having a conversation!

## Running the Example 🏃‍♀️

To run this example, make sure you have your OpenAI API key set up properly. Then, just run the program and watch as the AI suggests creative and colorful names for your hypothetical sock company!

So, are you ready to dive into the world of AI-generated sock company names? Let's go! 🚀🧦
17 changes: 17 additions & 0 deletions examples/openai-o1-example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/tmc/langchaingo/examples/openai-o1-example

go 1.22.0

toolchain go1.22.1

require github.com/tmc/langchaingo v0.1.13-0.20240912225956-cc420948ff14

require (
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pkoukk/tiktoken-go v0.1.6 // indirect
golang.org/x/sys v0.20.0 // indirect
)
43 changes: 43 additions & 0 deletions examples/openai-o1-example/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkoukk/tiktoken-go v0.1.6 h1:JF0TlJzhTbrI30wCvFuiw6FzP2+/bR+FIxUdgEAcUsw=
github.com/pkoukk/tiktoken-go v0.1.6/go.mod h1:9NiV+i9mJKGj1rYOT+njbv+ZwA/zJxYdewGl6qVatpg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tmc/langchaingo v0.1.12 h1:yXwSu54f3b1IKw0jJ5/DWu+qFVH1NBblwC0xddBzGJE=
github.com/tmc/langchaingo v0.1.12/go.mod h1:cd62xD6h+ouk8k/QQFhOsjRYBSA1JJ5UVKXSIgm7Ni4=
github.com/tmc/langchaingo v0.1.13-0.20240912224110-75c2ca402e07 h1:hqmdRnNNmJWKxljjEtJRLupEdScPCJzVOG4E7PMQQrg=
github.com/tmc/langchaingo v0.1.13-0.20240912224110-75c2ca402e07/go.mod h1:vjzUeUsmulZ7Hwq0Ju3Ez+ZmM5aw0dA7K7K2u7wooIw=
github.com/tmc/langchaingo v0.1.13-0.20240912224408-c578cf8de602 h1:Gd9yl+4HHBiu8e4IUMG621PwnwtmPhN/qP36jnXQ8Bw=
github.com/tmc/langchaingo v0.1.13-0.20240912224408-c578cf8de602/go.mod h1:vjzUeUsmulZ7Hwq0Ju3Ez+ZmM5aw0dA7K7K2u7wooIw=
github.com/tmc/langchaingo v0.1.13-0.20240912225044-81e166c821e6 h1:p3eNVZZLD5YgK6/01ektQie/K7kukXcFE/jM5aUK2cU=
github.com/tmc/langchaingo v0.1.13-0.20240912225044-81e166c821e6/go.mod h1:vjzUeUsmulZ7Hwq0Ju3Ez+ZmM5aw0dA7K7K2u7wooIw=
github.com/tmc/langchaingo v0.1.13-0.20240912225956-cc420948ff14 h1:1EXyMnFpAkyqjTjlHSSylimijeIAfdYBnQpsdRiVX18=
github.com/tmc/langchaingo v0.1.13-0.20240912225956-cc420948ff14/go.mod h1:vjzUeUsmulZ7Hwq0Ju3Ez+ZmM5aw0dA7K7K2u7wooIw=
github.com/tmc/langchaingo v0.1.13-0.20240912235243-91b7ed9872e4 h1:Z4poC1pYT49V/x8wH3/Bu3Y+qEDDq50OzFFehzgzxW8=
github.com/tmc/langchaingo v0.1.13-0.20240912235243-91b7ed9872e4/go.mod h1:vjzUeUsmulZ7Hwq0Ju3Ez+ZmM5aw0dA7K7K2u7wooIw=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
47 changes: 47 additions & 0 deletions examples/openai-o1-example/openai_o1_chat_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"

"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/openai"
)

var flagModel = flag.String("model", "o1-preview", "model to use (e.g. 'o1-preview', 'o1-mini')")

func main() {
flag.Parse()
llm, err := openai.New(
openai.WithModel(*flagModel),
)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
content := []llms.MessageContent{
llms.TextParts(llms.ChatMessageTypeHuman, `
I want to build a Go app that takes user questions and looks them up in a
database where they are mapped to answers. If there ia close match, it retrieves
the matched answer. If there isn't, it asks the user to provide an answer and
stores the question/answer pair in the database. Make a plan for the directory
structure you'll need, then return each file in full. Only supply your reasoning
at the beginning and end, not throughout the code.`),
}
fmt.Println("Generating content...")
output, err := llm.GenerateContent(ctx, content,
llms.WithMaxTokens(4000),
llms.WithTemperature(1),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(output.Choices[0].Content)

fmt.Println("\ngeneration info:")
json.NewEncoder(os.Stdout).Encode(output.Choices[0].GenerationInfo)
}

0 comments on commit f01a52c

Please sign in to comment.