gollm
เป็นแพ็คเกจ GO ที่ออกแบบมาเพื่อช่วยให้คุณสร้าง AI Golems ของคุณเอง เช่นเดียวกับที่ Golem Mystical ของตำนานถูกนำมาใช้ชีวิตด้วยคำพูดศักดิ์สิทธิ์ gollm
ช่วยให้คุณหายใจชีวิตในการสร้างสรรค์ AI ของคุณโดยใช้พลังของแบบจำลองภาษาขนาดใหญ่ (LLMs) แพ็คเกจนี้ทำให้การโต้ตอบกับผู้ให้บริการ LLM ต่าง ๆ ง่ายขึ้นซึ่งนำเสนออินเทอร์เฟซแบบครบวงจรที่ยืดหยุ่นและทรงพลังสำหรับวิศวกร AI และนักพัฒนาซอฟต์แวร์เพื่อสร้างคนรับใช้ดิจิทัลของตัวเอง
เอกสาร
ChainOfThought
สำหรับงานการใช้เหตุผลที่ซับซ้อน gollm
สามารถจัดการงาน AI ที่หลากหลาย ได้แก่ ::
ChainOfThought
เพื่อวิเคราะห์ปัญหาที่ซับซ้อนทีละขั้นตอนgo get github.com/teilomillet/gollm
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/teilomillet/gollm"
)
func main () {
// Load API key from environment variable
apiKey := os . Getenv ( "OPENAI_API_KEY" )
if apiKey == "" {
log . Fatalf ( "OPENAI_API_KEY environment variable is not set" )
}
// Create a new LLM instance with custom configuration
llm , err := gollm . NewLLM (
gollm . SetProvider ( "openai" ),
gollm . SetModel ( "gpt-4o-mini" ),
gollm . SetAPIKey ( apiKey ),
gollm . SetMaxTokens ( 200 ),
gollm . SetMaxRetries ( 3 ),
gollm . SetRetryDelay ( time . Second * 2 ),
gollm . SetLogLevel ( gollm . LogLevelInfo ),
)
if err != nil {
log . Fatalf ( "Failed to create LLM: %v" , err )
}
ctx := context . Background ()
// Create a basic prompt
prompt := gollm . NewPrompt ( "Explain the concept of 'recursion' in programming." )
// Generate a response
response , err := llm . Generate ( ctx , prompt )
if err != nil {
log . Fatalf ( "Failed to generate text: %v" , err )
}
fmt . Printf ( "Response: n %s n " , response )
}
## Quick Reference
Here 's a quick reference guide for the most commonly used functions and options in the `gollm` package :
### LLM Creation and Configuration
`` `go
llm, err := gollm.NewLLM(
gollm.SetProvider("openai"),
gollm.SetModel("gpt-4"),
gollm.SetAPIKey("your-api-key"),
gollm.SetMaxTokens(100),
gollm.SetTemperature(0.7),
gollm.SetMemory(4096),
)
prompt := gollm . NewPrompt ( "Your prompt text here" ,
gollm . WithContext ( "Additional context" ),
gollm . WithDirectives ( "Be concise" , "Use examples" ),
gollm . WithOutput ( "Expected output format" ),
gollm . WithMaxLength ( 300 ),
)
response , err := llm . Generate ( ctx , prompt )
response , err := tools . ChainOfThought ( ctx , llm , "Your question here" )
optimizer := optimizer . NewPromptOptimizer ( llm , initialPrompt , taskDescription ,
optimizer . WithCustomMetrics ( /* custom metrics */ ),
optimizer . WithRatingSystem ( "numerical" ),
optimizer . WithThreshold ( 0.8 ),
)
optimizedPrompt , err := optimizer . OptimizePrompt ( ctx )
results , err := tools . CompareModels ( ctx , promptText , validateFunc , configs ... )
แพ็คเกจ gollm
นำเสนอคุณสมบัติขั้นสูงที่หลากหลายเพื่อปรับปรุงแอปพลิเคชัน AI ของคุณ:
สร้างพรอมต์ที่ซับซ้อนด้วยส่วนประกอบหลายอย่าง:
prompt := gollm . NewPrompt ( "Explain the concept of recursion in programming." ,
gollm . WithContext ( "The audience is beginner programmers." ),
gollm . WithDirectives (
"Use simple language and avoid jargon." ,
"Provide a practical example." ,
"Explain potential pitfalls and how to avoid them." ,
),
gollm . WithOutput ( "Structure your response with sections: Definition, Example, Pitfalls, Best Practices." ),
gollm . WithMaxLength ( 300 ),
)
response , err := llm . Generate ( ctx , prompt )
if err != nil {
log . Fatalf ( "Failed to generate explanation: %v" , err )
}
fmt . Printf ( "Explanation of Recursion: n %s n " , response )
ใช้ฟังก์ชั่น ChainOfThought
สำหรับการใช้เหตุผลทีละขั้นตอน:
question := "What is the result of 15 * 7 + 22?"
response , err := tools . ChainOfThought ( ctx , llm , question )
if err != nil {
log . Fatalf ( "Failed to perform chain of thought: %v" , err )
}
fmt . Printf ( "Chain of Thought: n %s n " , response )
โหลดตัวอย่างโดยตรงจากไฟล์:
examples , err := utils . ReadExamplesFromFile ( "examples.txt" )
if err != nil {
log . Fatalf ( "Failed to read examples: %v" , err )
}
prompt := gollm . NewPrompt ( "Generate a similar example:" ,
gollm . WithExamples ( examples ... ),
)
response , err := llm . Generate ( ctx , prompt )
if err != nil {
log . Fatalf ( "Failed to generate example: %v" , err )
}
fmt . Printf ( "Generated Example: n %s n " , response )
สร้างเทมเพลตพรอมต์ที่นำกลับมาใช้ใหม่ได้สำหรับการสร้างพรอมต์ที่สอดคล้องกัน:
// Create a new prompt template
template := gollm . NewPromptTemplate (
"AnalysisTemplate" ,
"A template for analyzing topics" ,
"Provide a comprehensive analysis of {{.Topic}}. Consider the following aspects: n " +
"1. Historical context n " +
"2. Current relevance n " +
"3. Future implications" ,
gollm . WithPromptOptions (
gollm . WithDirectives (
"Use clear and concise language" ,
"Provide specific examples where appropriate" ,
),
gollm . WithOutput ( "Structure your analysis with clear headings for each aspect." ),
),
)
// Use the template to create a prompt
data := map [ string ] interface {}{
"Topic" : "artificial intelligence in healthcare" ,
}
prompt , err := template . Execute ( data )
if err != nil {
log . Fatalf ( "Failed to execute template: %v" , err )
}
// Generate a response using the created prompt
response , err := llm . Generate ( ctx , prompt )
if err != nil {
log . Fatalf ( "Failed to generate response: %v" , err )
}
fmt . Printf ( "Analysis: n %s n " , response )
ตรวจสอบให้แน่ใจว่าเอาต์พุต LLM ของคุณอยู่ในรูปแบบ JSON ที่ถูกต้อง:
prompt := gollm . NewPrompt ( "Analyze the pros and cons of remote work." ,
gollm . WithOutput ( "Respond in JSON format with 'topic', 'pros', 'cons', and 'conclusion' fields." ),
)
response , err := llm . Generate ( ctx , prompt , gollm . WithJSONSchemaValidation ())
if err != nil {
log . Fatalf ( "Failed to generate valid analysis: %v" , err )
}
var result AnalysisResult
if err := json . Unmarshal ([] byte ( response ), & result ); err != nil {
log . Fatalf ( "Failed to parse response: %v" , err )
}
fmt . Printf ( "Analysis: %+v n " , result )
ใช้ PromptOptimizer
เพื่อปรับแต่งและปรับปรุงพรอมต์ของคุณโดยอัตโนมัติ:
initialPrompt := gollm . NewPrompt ( "Write a short story about a robot learning to love." )
taskDescription := "Generate a compelling short story that explores the theme of artificial intelligence developing emotions."
optimizerInstance := optimizer . NewPromptOptimizer (
llm ,
initialPrompt ,
taskDescription ,
optimizer . WithCustomMetrics (
optimizer. Metric { Name : "Creativity" , Description : "How original and imaginative the story is" },
optimizer. Metric { Name : "Emotional Impact" , Description : "How well the story evokes feelings in the reader" },
),
optimizer . WithRatingSystem ( "numerical" ),
optimizer . WithThreshold ( 0.8 ),
optimizer . WithVerbose (),
)
optimizedPrompt , err := optimizerInstance . OptimizePrompt ( ctx )
if err != nil {
log . Fatalf ( "Optimization failed: %v" , err )
}
fmt . Printf ( "Optimized Prompt: %s n " , optimizedPrompt . Input )
เปรียบเทียบการตอบสนองจากผู้ให้บริการหรือแบบจำลอง LLM ที่แตกต่างกัน:
configs := [] * gollm. Config {
{
Provider : "openai" ,
Model : "gpt-4o-mini" ,
APIKey : os . Getenv ( "OPENAI_API_KEY" ),
MaxTokens : 500 ,
},
{
Provider : "anthropic" ,
Model : "claude-3-5-sonnet-20240620" ,
APIKey : os . Getenv ( "ANTHROPIC_API_KEY" ),
MaxTokens : 500 ,
},
{
Provider : "groq" ,
Model : "llama-3.1-70b-versatile" ,
APIKey : os . Getenv ( "GROQ_API_KEY" ),
MaxTokens : 500 ,
},
}
promptText := "Tell me a joke about programming. Respond in JSON format with 'setup' and 'punchline' fields."
validateJoke := func ( joke map [ string ] interface {}) error {
if joke [ "setup" ] == "" || joke [ "punchline" ] == "" {
return fmt . Errorf ( "joke must have both a setup and a punchline" )
}
return nil
}
results , err := tools . CompareModels ( context . Background (), promptText , validateJoke , configs ... )
if err != nil {
log . Fatalf ( "Error comparing models: %v" , err )
}
fmt . Println ( tools . AnalyzeComparisonResults ( results ))
เปิดใช้งานหน่วยความจำเพื่อรักษาบริบทในการโต้ตอบหลายครั้ง:
llm , err := gollm . NewLLM (
gollm . SetProvider ( "openai" ),
gollm . SetModel ( "gpt-3.5-turbo" ),
gollm . SetAPIKey ( os . Getenv ( "OPENAI_API_KEY" )),
gollm . SetMemory ( 4096 ), // Enable memory with a 4096 token limit
)
if err != nil {
log . Fatalf ( "Failed to create LLM: %v" , err )
}
ctx := context . Background ()
// First interaction
prompt1 := gollm . NewPrompt ( "What's the capital of France?" )
response1 , err := llm . Generate ( ctx , prompt1 )
if err != nil {
log . Fatalf ( "Failed to generate response: %v" , err )
}
fmt . Printf ( "Response 1: %s n " , response1 )
// Second interaction, referencing the first
prompt2 := gollm . NewPrompt ( "What's the population of that city?" )
response2 , err := llm . Generate ( ctx , prompt2 )
if err != nil {
log . Fatalf ( "Failed to generate response: %v" , err )
}
fmt . Printf ( "Response 2: %s n " , response2 )
วิศวกรรมที่รวดเร็ว :
NewPrompt()
พร้อมตัวเลือกเช่น WithContext()
, WithDirectives()
และ WithOutput()
เพื่อสร้างพรอมต์ที่มีโครงสร้างที่ดี prompt := gollm . NewPrompt ( "Your main prompt here" ,
gollm . WithContext ( "Provide relevant context" ),
gollm . WithDirectives ( "Be concise" , "Use examples" ),
gollm . WithOutput ( "Specify expected output format" ),
)
ใช้เทมเพลตพรอมต์ :
PromptTemplate
template := gollm . NewPromptTemplate (
"CustomTemplate" ,
"A template for custom prompts" ,
"Generate a {{.Type}} about {{.Topic}}" ,
gollm . WithPromptOptions (
gollm . WithDirectives ( "Be creative" , "Use vivid language" ),
gollm . WithOutput ( "Your {{.Type}}:" ),
),
)
ใช้ประโยชน์จากฟังก์ชั่นที่สร้างไว้ล่วงหน้า :
ChainOfThought()
สำหรับงานการใช้เหตุผลที่ซับซ้อน response , err := tools . ChainOfThought ( ctx , llm , "Your complex question here" )
ทำงานกับตัวอย่าง :
ReadExamplesFromFile()
เพื่อโหลดตัวอย่างจากไฟล์สำหรับเอาต์พุตที่สอดคล้องกัน examples , err := utils . ReadExamplesFromFile ( "examples.txt" )
if err != nil {
log . Fatalf ( "Failed to read examples: %v" , err )
}
ใช้เอาต์พุตที่มีโครงสร้าง :
WithJSONSchemaValidation()
เพื่อให้แน่ใจว่าเอาต์พุต JSON ที่ถูกต้อง response , err := llm . Generate ( ctx , prompt , gollm . WithJSONSchemaValidation ())
เพิ่มประสิทธิภาพพรอมต์ :
PromptOptimizer
เพื่อปรับแต่งพรอมต์โดยอัตโนมัติ optimizer := optimizer . NewPromptOptimizer ( llm , initialPrompt , taskDescription ,
optimizer . WithCustomMetrics (
optimizer. Metric { Name : "Relevance" , Description : "How relevant the response is to the task" },
),
optimizer . WithRatingSystem ( "numerical" ),
optimizer . WithThreshold ( 0.8 ),
)
เปรียบเทียบการแสดงโมเดล :
CompareModels()
เพื่อประเมินแบบจำลองหรือผู้ให้บริการที่แตกต่างกัน results , err := tools . CompareModels ( ctx , promptText , validateFunc , configs ... )
ใช้หน่วยความจำสำหรับการโต้ตอบตามบริบท :
llm , err := gollm . NewLLM (
gollm . SetProvider ( "openai" ),
gollm . SetModel ( "gpt-3.5-turbo" ),
gollm . SetMemory ( 4096 ),
)
การจัดการข้อผิดพลาดและการลองใหม่ :
llm , err := gollm . NewLLM (
gollm . SetMaxRetries ( 3 ),
gollm . SetRetryDelay ( time . Second * 2 ),
)
การจัดการคีย์ API ที่ปลอดภัย :
llm , err := gollm . NewLLM (
gollm . SetAPIKey ( os . Getenv ( "OPENAI_API_KEY" )),
)
ตรวจสอบไดเรกทอรีตัวอย่างของเราสำหรับตัวอย่างการใช้งานเพิ่มเติมรวมถึง:
gollm
ได้รับการดูแลอย่างแข็งขันและอยู่ภายใต้การพัฒนาอย่างต่อเนื่อง ด้วย refactoring ล่าสุดเราได้ปรับปรุง codebase เพื่อให้ง่ายขึ้นและเข้าถึงได้ง่ายขึ้นสำหรับผู้มีส่วนร่วมใหม่ เรายินดีต้อนรับการมีส่วนร่วมและข้อเสนอแนะจากชุมชน
gollm
สร้างขึ้นบนปรัชญาของความเรียบง่ายในทางปฏิบัติและความเรียบง่ายในการคิดล่วงหน้า:
สร้างสิ่งที่จำเป็น : เราเพิ่มคุณสมบัติตามที่ต้องการหลีกเลี่ยงการพัฒนาแบบเก็งกำไร
ความเรียบง่ายก่อน : การเพิ่มเติมควรตรงไปตรงมาในขณะที่บรรลุวัตถุประสงค์
เข้ากันได้ในอนาคต : เราพิจารณาว่าการเปลี่ยนแปลงในปัจจุบันอาจส่งผลกระทบต่อการพัฒนาในอนาคตอย่างไร
จำนวนการอ่าน : รหัสควรมีความชัดเจนและอธิบายตนเอง
การออกแบบแบบแยกส่วน : แต่ละองค์ประกอบควรทำสิ่งหนึ่งได้ดี
เรายินดีต้อนรับการมีส่วนร่วมที่สอดคล้องกับปรัชญาของเรา! ไม่ว่าคุณจะแก้ไขข้อผิดพลาดการปรับปรุงเอกสารหรือเสนอคุณสมบัติใหม่ความพยายามของคุณจะได้รับการชื่นชม
เพื่อเริ่มต้น:
ขอบคุณที่ช่วยให้ gollm
ดีขึ้น!
โครงการนี้ได้รับอนุญาตภายใต้ Apache License 2.0 - ดูไฟล์ใบอนุญาตสำหรับรายละเอียด