Skip to content

Temperature 0 not really working out #32

@alarbada

Description

@alarbada

Hello there. I don't know if this is a bug specific to this library or openai api itself, but I only encounter this problem with go-gpt3.

The following code will output different generations for 0 temperature.

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/PullRequestInc/go-gpt3"
)

func onErrPanic(err *error) {
	if *err != nil {
		panic((*err).Error())
	}
}

func generate() (err error) {
	defer onErrPanic(&err)

	ctx := context.Background()
	client := gpt3.NewClient("your own api key")

	var completion string

	content := fmt.Sprintf(`
-- This is the postgresql schema

%v


-- here are the queries. They have a name, annotated by ":name". They
-- also have another tag that represents how many rows should be returned.
-- :one means that only one row is expected to be returned
-- :many means that more than one row will be returned

%v


-- Write go functions that consume the previous queries. You won't write queries using a specific driver.
-- Rather, the queries will be provided an interface that abstracts the database.
-- That interface won't have a constructor. You won't write a main function. You won't write comments. The
-- package will be main.
`, exampleSchema, exampleQueries)

	err = client.ChatCompletionStream(ctx, gpt3.ChatCompletionRequest{
		Model: gpt3.GPT3Dot5Turbo,
		Messages: []gpt3.ChatCompletionRequestMessage{
			{
				Role:    "system",
				Content: `You are a command line tool that generates golang code contacting the database. You only output go code, nothing else`,
			},
			{
				Role: "user",
				Content: content,
			},
		},
		Temperature:      0, // note this, temperature is set to 0
		Stream:           true,
		MaxTokens:        1000,
	}, func(res *gpt3.ChatCompletionStreamResponse) {
		fmt.Print(res.Choices[0].Delta.Content)
		completion += res.Choices[0].Delta.Content
	})
	if err != nil {
		return err
	}

	fmt.Println()

	return nil
}

var exampleSchema = `
CREATE TABLE authors (
    id   BIGSERIAL PRIMARY KEY,
    name text      NOT NULL,
    bio  text
);
`

var exampleQueries = `
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;
`

func main() {
	generate()
}

Output:

image

Here's the equivalent in the python openai sdk, with temperature equivalent to 0 I get consistent results:

import openai

openai.api_key = "your api key"

def gen_prompt(schema: str, queries: str):
    return f"""
-- This is the postgresql schema

{schema}


-- here are the queries. They have a name, annotated by ":name". They
-- also have another tag that represents how many rows should be returned.
-- :one means that only one row is expected to be returned
-- :many means that more than one row will be returned

{queries}


-- Write go functions that consume the previous queries. You won't write queries using a specific driver.
-- Rather, the queries will be provided an interface that abstracts the database.
-- That interface won't have a constructor. You won't write a main function. You won't write comments. The
-- package will be main.
"""

exampleSchema = """
CREATE TABLE authors (
    id   BIGSERIAL PRIMARY KEY,
    name text      NOT NULL,
    bio  text
);
"""

exampleQueries = """
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;
"""


def main():
    prompt = gen_prompt(exampleSchema, exampleQueries)

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages = [
			{
				"role":    "system",
				"content": "You are a command line tool that generates golang code contacting the database. You only output go code, nothing else",
			},
			{
				"role": "user",
				"content": prompt,
			},
        ],
        max_tokens = 1000,
        temperature = 0,
        stream = True
    )

    for completion in response:
        try:
            content = completion["choices"][0]["delta"]["content"]
            # print content unbuffered without printing newlines
            print(content, end="", flush=True)
        except:
            pass

main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions