Code Monkey home page Code Monkey logo

mistral-go's Introduction

Mistral Go Client

Go Reference Go Report Card

Unofficial Golang client library for Mistral AI platform

Installation

go get github.com/robertjkeck2/mistral-go

Requires Go 1.21 or later

Usage

Mistral Chat Completion example:

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/robertjkeck2/go-mistral"
)

func main() {
	client := mistral.NewMistralClient(os.Getenv("MISTRAL_API_KEY"))
	resp, err := client.CreateChatCompletion(
		context.Background(),
		mistral.ChatCompletionRequest{
			Model:    "mistral-tiny",
			Messages: []mistral.ChatMessage{{Role: mistral.RoleUser, Content: "What is the best French cheese?"}},
		},
	)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(resp.Choices[0].Message.Content)
}

Getting a Mistral API key:

  1. Sign up for a Mistral account at https://console.mistral.ai/.
  2. If you don't already have one, create an account.
  3. In the Your subscription section, click Manage.
  4. Update your billing information in the Billing information section.
  5. Create a new subscription in the Current subscription section.
  6. Go to the API Keys tab in the left sidebar.
  7. Click Generate a new key and copy the key. Save the key somewhere safe and do not share this key with anyone.

Other examples:

Chat Completion Streaming
package main

import (
	"context"
	"errors"
	"fmt"
	"io"
	"os"

	"github.com/robertjkeck2/go-mistral"
)

func main() {
	client := mistral.NewMistralClient(os.Getenv("MISTRAL_API_KEY"))
	stream, err := client.CreateChatCompletionStream(
		context.Background(),
		mistral.ChatCompletionRequest{
			Model:    "mistral-tiny",
			Messages: []mistral.ChatMessage{{Role: mistral.RoleUser, Content: "What is the best French cheese?"}},
		},
	)
	if err != nil {
		fmt.Println(err)
		return
	}

	defer stream.Close()

	for {
		var response mistral.ChatCompletionStreamResponse
		response, err = stream.Recv()
		if errors.Is(err, io.EOF) {
			return
		}

		if err != nil {
			fmt.Println(err)
			return
		}

		fmt.Printf(response.Choices[0].Delta.Content)
	}
}
Embeddings
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/robertjkeck2/go-mistral"
)

func main() {
	client := mistral.NewMistralClient(os.Getenv("MISTRAL_API_KEY"))
	resp, err := client.CreateEmbedding(
		context.Background(),
		mistral.EmbeddingRequest{
			Model: "mistral-embed",
			Input: []string{"What is the best French cheese?"},
		},
	)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(resp.Data[0].Embedding)
}
List Models
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/robertjkeck2/go-mistral"
)

func main() {
	client := mistral.NewMistralClient(os.Getenv("MISTRAL_API_KEY"))
	resp, err := client.ListModels(context.Background())
	if err != nil {
		fmt.Println(err)
		return
	}
	if len(resp.Data) == 0 {
		fmt.Println("No models found")
		return
	}
	for _, model := range resp.Data {
		fmt.Println(model.ID)
	}
}

Mistral API documentation

https://docs.mistral.ai/api

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for more information.

License

This project is licensed under the MIT License - see the LICENSE file for details.

"Buy Me A Coffee"

mistral-go's People

Contributors

robertjkeck2 avatar kaatinga avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.