twang2218 / go-langchain

A Go implementation of LangChain

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ¦œοΈπŸ”— Go LangChain

⚑ Building applications with LLMs through composability ⚑

Notice: The library is in a very early stage of development.

πŸ“¦ Installation

go get github.com/lab99x/go-langchain

πŸ€” What is this?

Large language models (LLMs) are emerging as a transformative technology, enabling developers to build applications that they previously could not. But using these LLMs in isolation is often not enough to create a truly powerful app - the real power comes when you can combine them with other sources of computation or knowledge.

This library is aimed at assisting in the development of those types of applications.

πŸ“– Full Documentation

TODO: Add link to full documentation

πŸš€ Quick Start

To call a LLM, such as OpenAI GPT API, you can use the following code:

package main

import (
    "context"
    "fmt"

    "github.com/lab99x/go-langchain/llms"
)

func main() {
    llm := &llms.OpenAI{} // OpenAI API key can be set in environment variable OPENAI_API_KEY
    resp, err := llm.Chat(context.Background(), "When the forbidden city was built?")
    if err != nil {
        panic(err)
    }
    fmt.Println(resp)
    // Output: The forbidden city in Beijing was built in 1406.
}

The Chains and Prompts are also implemented in this library. For example, you can use the following code to create a chain that calls OpenAI GPT API:

package main

import (
    "context"
    "fmt"

    "github.com/lab99x/go-langchain/llms"
    "github.com/lab99x/go-langchain/prompts"
    "github.com/lab99x/go-langchain/chains"
)

func main() {
    llm := &llms.OpenAI{} // OpenAI API key can be set in environment variable OPENAI_API_KEY
    prompt_template := prompts.NewPromptTemplateByTemplate("Where is the capital city of {country}?")
    c := chains.NewLLMChain(llm, prompt_template)

    resp, err := c.RunText(context.Background(), "China")
    if err != nil {
        panic(err)
    }
    fmt.Println(resp)
    // Output: The capital city of China is Beijing.

    resp, err = c.Run(context.Background(), map[string]string{"country": "France"})
    if err != nil {
        panic(err)
    }
    fmt.Println(resp)
    // Output: The capital city of France is Paris.
}

Relationship with Python LangChain

This library is the Go language implementation of LangChain.

The LangChainHub is a central place for the serialized versions of these prompts, chains, and agents.

πŸ’ Contributing

As an open source project in a rapidly developing field, we are extremely open to contributions, whether it be in the form of a new feature, improved infra, or better documentation.

TODO: Add contributing guidelines

About

A Go implementation of LangChain

License:MIT License


Languages

Language:Go 99.6%Language:Makefile 0.4%