Created
April 24, 2023 19:40
-
-
Save ChristianSch/20251608147883cd5c6752988f16eb8e to your computer and use it in GitHub Desktop.
main.go to test openAI chat generation with langchaingo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/tmc/langchaingo/chatmodels/openai" | |
"github.com/tmc/langchaingo/exp/chains" | |
"github.com/tmc/langchaingo/exp/prompts" | |
) | |
func main() { | |
chat, err := openai.New() // FIXME: should probably not use the same API as the openAI LLM | |
if err != nil { | |
log.Fatal(err) | |
} | |
systemTemplate, err := prompts.NewPromptTemplate( | |
"You are a helpful assistant that translates {input_language} to {output_language}.", | |
[]string{"input_language", "output_language"}, | |
) | |
if err != nil { | |
log.Fatal(err) | |
} | |
userTemplate, err := prompts.NewPromptTemplate("{text}", []string{"text"}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
chatPrompt, err := prompts.NewChatTemplate([]prompts.Message{ | |
prompts.NewSystemMessage(systemTemplate), | |
prompts.NewHumanMessage(userTemplate), | |
}, []string{"input_language", "output_language", "text"}) | |
chainB := chains.NewLLMChain(chat, chatPrompt) | |
resB, err := chains.Call(chainB, map[string]any{ | |
"input_language": "English", | |
"output_language": "French", | |
"text": "I love programming", | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(resB) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment