palm

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2023 License: MIT Imports: 7 Imported by: 2

README

Palm API Go Client

This package provides a Go client for the Palm API. The PaLM API allows developers to build generative AI applications using the PaLM model.

Installation

Install the package on your system

go get github.com/mr-destructive/palm

Usage

To use this package, you'll need a Palm API key. You can get one from the Google Cloud Console.

Set your API key in an .env file:

PALM_API_KEY=YOUR_KEY_HERE

or in the shell:

export PALM_API_KEY=YOUR_KEY_HERE

Import the packge with the name github.com/mr-destructive/palm as :

package main

import (
  "github.com/mr-destructive/palm"
)

Models

Then you can list models with ListModels():

models, err := palm.ListModels()
if err != nil {
    log.Fatal(err)
}
fmt.Println(models)

And get a single model by name with GetModel(name), there are three model names as :

  • models/chat-bison-001
  • models/text-bison-001
  • models/embedding-gecko-001
model, err := palm.GetModel("model/chat-bison-001")
if err != nil {
    log.Fatal(err)
}
fmt.Println(model)
Chat

Get a quick conversation prompt with ChatPrompt(string) and further prompts with Reply(string) method

chat, err := palm.ChatPrompt("what are you?")
if err != nil {
    panic(err)
}
fmt.Println(chat.Last)
chat.Reply("what can you do!")
fmt.Println(chat.Last)

OR fine-tune the ChatConfig to the Chat method.

chatConfig := palm.ChatConfig{
    Prompt: palm.TextPrompt{
        Text: "what are you?",
    }
    Messages: []palm.Message{
        palm.Message{
            Author: "bot",
            Content: "hello world!",
        },
    },
    Model: "string"
    Context: "string",
    Examples: []palm.Example{
        palm.Example{
            Input: "what are you?",
            Output: "I am a bot",
        }, 
    },
    Temperature: 0.5,
    CandidateCount: 10,
    TopP: 0.5,
    TopK: 10,
}

chat, err := palm.Chat(chatConfig)
if err != nil {
    panic(err)
}
chat.Reply("what can you do!")
Generation

Use the underlying method in the Chat methods to get a ResponseMessage.

message := palm.MessagePrompt{
    Messages: []palm.Message{
        palm.Message{
            Content: "what is the meaning of life",
        },
    },
}
msgConfig := MessageConfig{
    Prompt: message,
}
m, err := palm.GenerateMessage(msgConfig)
if err != nil {
    fmt.Println(err)
}
fmt.Println(m)

Contributing

Contributions are welcome! Open an issue or submit a PR.

Documentation

Index

Constants

View Source
const API_BASE_URL string = "https://generativelanguage.googleapis.com/v1beta2"
View Source
const CHAT_MODEL string = "models/chat-bison-001"
View Source
const EMBED_MODEL string = "models/embed-gecko-001"
View Source
const TEXT_MODEL string = "models/text-bison-001"

Variables

This section is empty.

Functions

func GenerateText

func GenerateText(model string, params PromptConfig) (string, error)

Types

type ChatConfig

type ChatConfig struct {
	Model          string     `json:"model;omitempty"`
	Context        string     `json:"context;omitempty"`
	Examples       []Example  `json:"examples,omitempty"`
	Messages       []Message  `json:"messages;omitempty"`
	Temperature    float64    `json:"temperature;omitempty"`
	CandidateCount int        `json:"candidateCount;omitempty"`
	TopP           float64    `json:"topP;omitempty"`
	TopK           int        `json:"topK;omitempty"`
	Prompt         TextPrompt `json:"prompt"`
}

type ChatResponse

type ChatResponse struct {
	Candidates     []Message       `json:"candidates"`
	Filters        []ContentFilter `json:"filters"`
	Messages       []Message       `json:"messages"`
	Model          string          `json:"model"`
	Context        string          `json:"context"`
	Examples       []Example       `json:"examples"`
	Temperature    float64         `json:"temperature"`
	CandidateCount int             `json:"candidate_count"`
	TopK           int             `json:"top_k"`
	TopP           float64         `json:"top_p"`
	Last           string          `json:"last"`
}

func Chat

func Chat(config ChatConfig) (ChatResponse, error)

func ChatPrompt

func ChatPrompt(prompt string) (ChatResponse, error)

func (*ChatResponse) GetLast

func (c *ChatResponse) GetLast() string

func (*ChatResponse) Reply

func (c *ChatResponse) Reply(msg string)

type CitationMetadata

type CitationMetadata struct {
	CitationSource []CitationSource `json:"citationSource,omitempty"`
}

type CitationSource

type CitationSource struct {
	StartIndex int    `json:"startIndex,omitempty"`
	EndIndex   int    `json:"endIndex,omitempty"`
	Uri        string `json:"uri,omitempty"`
	License    string `json:"license,omitempty"`
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(authToken string) *Client

func (*Client) ChatPrompt

func (c *Client) ChatPrompt(prompt string) (ResponseText, error)

type ClientConfig

type ClientConfig struct {
	BaseURL    string
	OrgID      string
	APIVersion string
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

type ContentFilter

type ContentFilter struct {
	Reason  string `json:"reason"`
	Message string `json:"message"`
}

type Embedding

type Embedding struct {
	Value []float64 `json:"value"`
}

type Example

type Example struct {
	Input  Message `json:"input,omitempty"`
	Output Message `json:"output,omitempty"`
}

type Message

type Message struct {
	Author           string           `json:"author"`
	Content          string           `json:"content"`
	CitationMetadata CitationMetadata `json:"citationMetadata,omitempty"`
}

type MessageConfig

type MessageConfig struct {
	Prompt         MessagePrompt `json:"prompt"`
	Temperature    float64       `json:"temperature"`
	CandidateCount int           `json:"candidateCount"`
	TopP           float64       `json:"topP"`
	TopK           int           `json:"topK"`
}

type MessagePrompt

type MessagePrompt struct {
	Context  string    `json:"context"`
	Examples []Example `json:"examples,omitempty"`
	Messages []Message `json:"messages"`
}

type Model

type Model struct {
	Name             string   `json:"name"`
	BaseModelId      string   `json:"baseModelId"`
	Version          string   `json:"version"`
	DisplayName      string   `json:"displayName"`
	Description      string   `json:"description"`
	InputTokenLimit  int      `json:"inputTokenLimit"`
	OutputTokenLimit int      `json:"outputTokenLimit"`
	SupportedMethods []string `json:"supportedGenerationMethods"`
	Temperature      float64  `json:"temperature"`
	TopP             float64  `json:"topP"`
	TopK             int      `json:"topK"`
}

func GetModel

func GetModel(name string) (Model, error)

GetModel returns a single model by name from the Palm API

func ListModels

func ListModels() ([]Model, error)

ListModels returns a list of all models from the Palm API

type PromptConfig

type PromptConfig struct {
	Prompt          TextPrompt      `json:"prompt"`
	SafetySettings  []SafetySetting `json:"safetySettings"`
	StopSequences   []string        `json:"stopSequences"`
	Temperature     float64         `json:"temperature"`
	CandidateCount  int             `json:"candidateCount"`
	MaxOutputTokens int             `json:"maxOutputTokens"`
	TopP            float64         `json:"topP"`
	TopK            int             `json:"topK"`
}

type ResponseChat

type ResponseChat struct {
	Candidates     []TextCompletion `json:"candidates"`
	Filters        []ContentFilter  `json:"filters"`
	SafetyFeedback []SafetyFeedback `json:"safetyFeedback"`
}

type ResponseEmbed

type ResponseEmbed struct {
	Embedding Embedding `json:"embedding"`
}

func EmbedText

func EmbedText(text string) (ResponseEmbed, error)

type ResponseMessage

type ResponseMessage struct {
	Candidates []Message       `json:"candidates"`
	Messages   []Message       `json:"messages"`
	Filters    []ContentFilter `json:"filters"`
}

func GenerateMessage

func GenerateMessage(messageConfig MessageConfig) (*ResponseMessage, error)

type ResponseText

type ResponseText struct {
	Candidates []TextCompletion `json:"candidates"`
}

type SafetyFeedback

type SafetyFeedback struct {
	Rating  SafetyRating  `json:"rating"`
	Setting SafetySetting `json:"setting"`
}

type SafetyRating

type SafetyRating struct {
	Category  string  `json:"category"`
	Threshold float64 `json:"threshold"`
}

type SafetySetting

type SafetySetting struct {
	StopSequence string `json:"stopSequence"`
}

type TextCompletion

type TextCompletion struct {
	Output           string           `json:"output"`
	SafetyRatings    []SafetyRating   `json:"safetyRatings"`
	CitationMetadata CitationMetadata `json:"citationMetadata,omitempty"`
}

type TextPrompt

type TextPrompt struct {
	Text string `json:"text"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL