Documentation
¶
Index ¶
- Variables
- func CosineSimilarity(vec1, vec2 []float64) float64
- func DownloadPDF(pdfURL string) (string, error)
- func EstimateTokens(text string) int
- func ExtractDescriptions(content string) []string
- func ExtractRelevantText(extractedText string, index int, chunkSize int) string
- func ExtractRelevantText1(extractedText string, index int, chunkSize int) string
- func ExtractTextFromPDF(path string) (string, error)
- func FindMostSimilarChunk(queryEmbedding []float64, docEmbeddings [][]float64) int
- func FlattenAndConvertToFloat32(embeddings [][]float64) ([]float32, []int)
- func ReconstructToFloat64(flattened []float32, lengths []int) [][]float64
- func SplitTextIntoChunks(text string, chunkSize int, chunkOverlap int) []string
- type Agent
- type AgentConfig
- type Manager
- func (m *Manager) AssignTaskToAgent(agentID, taskID string)
- func (m *Manager) CreateAgent(id, name string, dependsOn []string) *Agent
- func (m *Manager) CreateTask(id, name string, toolID string, inputs map[string]interface{}) *Task
- func (m *Manager) ExecuteAllWorkflows() error
- func (m *Manager) ExecuteWorkflow() error
- func (m *Manager) InitializeWorkflow(config WorkflowConfig) error
- type OpenAIResponse
- type Task
- type TaskConfig
- type Tool
- type WorkflowConfig
Constants ¶
This section is empty.
Variables ¶
View Source
var ( TextToPDFTool = &Tool{ ID: "text_to_pdf", Name: "Text to PDF", Execute: func(inputs map[string]interface{}) (interface{}, <-chan interface{}, error) { text, ok := inputs["text"].(string) if !ok { return nil, nil, fmt.Errorf("input 'text' is required and must be a string") } fmt.Println("Converting text to PDF...") return fmt.Sprintf("PDF Content from: %s", text), nil, nil }, } OpenAIContentGeneratorTool = &Tool{ ID: "openai_content_generator", Name: "OpenAI Content Generator", Execute: func(inputs map[string]interface{}) (interface{}, <-chan interface{}, error) { query, ok := inputs["query"].(string) if !ok { return nil, nil, fmt.Errorf("input 'query' is required and must be a string") } chunkSize, ok := inputs["chunkSize"].(int) if !ok { return nil, nil, fmt.Errorf("input 'chunkSize' is required and must be an int") } chunkOverlap, ok := inputs["chunkOverlap"].(int) if !ok { return nil, nil, fmt.Errorf("input 'chunkOverlap' is required and must be an int") } context, ok := inputs["context"].(string) if !ok { return nil, nil, fmt.Errorf("input 'context' is required and must be a string") } fmt.Println(context) apiKey, ok := inputs["api_key"].(string) if !ok { return nil, nil, fmt.Errorf("input 'api_key' is required and must be a string") } verbose, _ := inputs["verbose"].(bool) model := "gpt-3.5-turbo" if m, ok := inputs["model"].(string); ok && m != "" { model = m } contextChunks := SplitTextIntoChunks(context, chunkSize, chunkOverlap) prompt := fmt.Sprintf("Context: %s\n\nQuery: %s", contextChunks[0], query) if EstimateTokens(prompt) > maxTokens { prompt = truncateTextToTokenLimit(prompt, maxTokens-500) } if verbose { log.Printf("Generated prompt: %s", prompt) } data := map[string]interface{}{ "model": model, "stream": true, "messages": []map[string]string{ {"role": "user", "content": prompt}, }, } jsonData, err := json.Marshal(data) if err != nil { return nil, nil, fmt.Errorf("failed to marshal request data: %v", err) } req, err := http.NewRequest("POST", "https://api.openai.com/v1/chat/completions", bytes.NewBuffer(jsonData)) if err != nil { return nil, nil, fmt.Errorf("failed to create request: %v", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+apiKey) client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, nil, fmt.Errorf("failed to execute request: %v", err) } contentChannel := make(chan interface{}) go func() { defer resp.Body.Close() defer close(contentChannel) reader := bufio.NewReader(resp.Body) for { line, err := reader.ReadString('\n') if err != nil { if err == io.EOF { break } log.Printf("failed to read stream: %v", err) break } if strings.TrimSpace(line) == "data: [DONE]" { break } if strings.HasPrefix(line, "data: ") { line = line[len("data: "):] } var streamResponse struct { Choices []struct { Delta struct { Content string `json:"content"` } `json:"delta"` } `json:"choices"` } if err := json.Unmarshal([]byte(line), &streamResponse); err != nil { continue } if len(streamResponse.Choices) > 0 { content := streamResponse.Choices[0].Delta.Content contentChannel <- content } } }() return nil, contentChannel, nil }, } ImageGeneratorTool = &Tool{ ID: "image_generator", Name: "Image Generator", Execute: func(inputs map[string]interface{}) (interface{}, <-chan interface{}, error) { description, ok := inputs["description"].(string) if !ok { return nil, nil, fmt.Errorf("input 'description' is required and must be a string") } apiKey, ok := inputs["api_key"].(string) if !ok { return nil, nil, fmt.Errorf("input 'api_key' is required and must be a string") } verbose, _ := inputs["verbose"].(bool) data := map[string]interface{}{ "prompt": description, "n": 1, "size": "1024x1024", } if verbose { log.Printf("Generating image with description: %s", description) } jsonData, err := json.Marshal(data) if err != nil { return nil, nil, fmt.Errorf("failed to marshal request data: %v", err) } req, err := http.NewRequest("POST", "https://api.openai.com/v1/images/generations", bytes.NewBuffer(jsonData)) if err != nil { return nil, nil, fmt.Errorf("failed to create request: %v", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+apiKey) client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, nil, fmt.Errorf("failed to execute request: %v", err) } defer resp.Body.Close() var response struct { Data []struct { URL string `json:"url"` } `json:"data"` } err = json.NewDecoder(resp.Body).Decode(&response) if err != nil { return nil, nil, fmt.Errorf("failed to decode response: %v", err) } if len(response.Data) == 0 { return nil, nil, fmt.Errorf("no images returned from OpenAI API") } if verbose { log.Printf("Generated image URL: %s", response.Data[0].URL) } return response.Data[0].URL, nil, nil }, } QueryToEmbeddingTool = &Tool{ ID: "query_to_embedding", Name: "Query to Embedding", Execute: func(inputs map[string]interface{}) (interface{}, <-chan interface{}, error) { query, ok := inputs["query"].(string) if !ok { return nil, nil, fmt.Errorf("input 'query' is required and must be a string") } apiKey, ok := inputs["api_key"].(string) if !ok { return nil, nil, fmt.Errorf("input 'api_key' is required and must be a string") } verbose, _ := inputs["verbose"].(bool) model := "text-embedding-ada-002" if m, ok := inputs["model"].(string); ok && m != "" { model = m } data := map[string]interface{}{ "model": model, "input": query, } jsonData, err := json.Marshal(data) if err != nil { return nil, nil, fmt.Errorf("failed to marshal request data: %v", err) } req, err := http.NewRequest("POST", "https://api.openai.com/v1/embeddings", bytes.NewBuffer(jsonData)) if err != nil { return nil, nil, fmt.Errorf("failed to create request: %v", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+apiKey) if verbose { log.Printf("Sending query to OpenAI Embedding API: %s", query) } client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, nil, fmt.Errorf("failed to execute request: %v", err) } defer resp.Body.Close() var response struct { Data []struct { Embedding []float64 `json:"embedding"` } `json:"data"` } err = json.NewDecoder(resp.Body).Decode(&response) if err != nil { return nil, nil, fmt.Errorf("failed to decode response: %v", err) } if len(response.Data) == 0 { return nil, nil, fmt.Errorf("no embeddings returned from OpenAI API") } if verbose { log.Printf("Query embedding generated successfully, embedding length: %d", len(response.Data[0].Embedding)) } return response.Data[0].Embedding, nil, nil }, } PDFToEmbeddingsTool = &Tool{ ID: "pdf_to_embeddings", Name: "PDF to Embeddings", Execute: func(inputs map[string]interface{}) (interface{}, <-chan interface{}, error) { pdfContent, _ := inputs["pdf_content"].(string) log.Println("PDF CONTENT LENGTH => " + fmt.Sprintf("%d", len(pdfContent))) chunkSize, _ := inputs["chunkSize"].(int) chunkOverlap, ok := inputs["chunkOverlap"].(int) if !ok { return nil, nil, fmt.Errorf("input 'chunkOverlap' is required and must be an int") } apiKey, ok := inputs["api_key"].(string) if !ok { return nil, nil, fmt.Errorf("input 'api_key' is required and must be a string") } verbose, _ := inputs["verbose"].(bool) chunks := SplitTextIntoChunks(pdfContent, chunkSize, chunkOverlap) var allEmbeddings [][]float64 for i, chunk := range chunks { if verbose { log.Printf("Processing chunk %d: %s", i+1, chunk) } if len(chunk) == 0 { if verbose { log.Printf("Chunk %d is empty, skipping.", i+1) } continue } data := map[string]interface{}{ "model": "text-embedding-ada-002", "input": chunk, } jsonData, err := json.Marshal(data) if err != nil { return nil, nil, fmt.Errorf("failed to marshal request data: %v", err) } req, err := http.NewRequest("POST", "https://api.openai.com/v1/embeddings", bytes.NewBuffer(jsonData)) if err != nil { return nil, nil, fmt.Errorf("failed to create request: %v", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+apiKey) client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, nil, fmt.Errorf("failed to execute request: %v", err) } defer resp.Body.Close() var response struct { Data []struct { Embedding []float64 `json:"embedding"` } `json:"data"` } err = json.NewDecoder(resp.Body).Decode(&response) if err != nil { return nil, nil, fmt.Errorf("failed to decode response: %v", err) } if len(response.Data) == 0 { if verbose { log.Printf("Failed to produce embeddings for the provided text chunk: %s", chunk) } return nil, nil, fmt.Errorf("no embeddings returned from OpenAI API") } allEmbeddings = append(allEmbeddings, response.Data[0].Embedding) if verbose { log.Printf("Chunk %d processed successfully, embedding length: %d", i+1, len(response.Data[0].Embedding)) } } if len(allEmbeddings) == 0 { return nil, nil, fmt.Errorf("no valid embeddings were produced") } if verbose { log.Printf("Total embeddings generated: %d", len(allEmbeddings)) } return allEmbeddings, nil, nil }, } PDFExtractorTool = &Tool{ ID: "pdf_extractor", Name: "PDF Extractor", Execute: func(inputs map[string]interface{}) (interface{}, <-chan interface{}, error) { pdfURL, ok := inputs["pdf_url"].(string) if !ok { return nil, nil, fmt.Errorf("input 'pdf_url' is required and must be a string") } verbose, _ := inputs["verbose"].(bool) pdfFilePath, err := DownloadPDF(pdfURL) if err != nil { return nil, nil, fmt.Errorf("failed to download PDF: %v", err) } defer os.Remove(pdfFilePath) text, err := ExtractTextFromPDF(pdfFilePath) if err != nil { return nil, nil, fmt.Errorf("failed to extract text from PDF: %v", err) } if verbose { log.Println("Extracted text from PDF:", text) } return text, nil, nil }, } ImageNeedCheckerTool = &Tool{ ID: "image_need_checker", Name: "Image Need Checker", Execute: func(inputs map[string]interface{}) (interface{}, <-chan interface{}, error) { content, ok := inputs["content"].(string) if !ok { return nil, nil, fmt.Errorf("input 'content' is required and must be a string") } apiKey, ok := inputs["api_key"].(string) if !ok { return nil, nil, fmt.Errorf("input 'api_key' is required and must be a string") } model := "text-embedding-ada-002" if m, ok := inputs["model"].(string); ok && m != "" { model = m } data := map[string]interface{}{ "model": model, "messages": []map[string]string{ {"role": "system", "content": "You are an assistant that identifies the need for diagrams or flowcharts in text."}, {"role": "user", "content": fmt.Sprintf("Given the following content, identify if any diagrams or flowcharts are needed and provide descriptions: %s. THE OUTPUT TO BE STRICTLY A SLICE OF STRINGS OR ARRAY OF STRINGS", content)}, }, } jsonData, err := json.Marshal(data) if err != nil { return nil, nil, fmt.Errorf("failed to marshal request data: %v", err) } req, err := http.NewRequest("POST", "https://api.openai.com/v1/chat/completions", bytes.NewBuffer(jsonData)) if err != nil { return nil, nil, fmt.Errorf("failed to create request: %v", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+apiKey) client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, nil, fmt.Errorf("failed to execute request: %v", err) } defer resp.Body.Close() var response OpenAIResponse err = json.NewDecoder(resp.Body).Decode(&response) if err != nil { return nil, nil, fmt.Errorf("failed to decode response: %v", err) } if len(response.Choices) == 0 { return nil, nil, fmt.Errorf("no choices returned from OpenAI API") } return response.Choices[0].Message.Content, nil, nil }, } )
Functions ¶
func CosineSimilarity ¶
func DownloadPDF ¶
func EstimateTokens ¶
func ExtractDescriptions ¶
func ExtractRelevantText ¶
func ExtractRelevantText1 ¶
func ExtractTextFromPDF ¶
func FindMostSimilarChunk ¶
func ReconstructToFloat64 ¶
Types ¶
type Agent ¶
type Agent struct {
ID string
Name string
Tasks []*Task
DependsOn []string
Output map[string]interface{}
Stream <-chan interface{}
}
func (*Agent) ExecuteTasks ¶
type AgentConfig ¶
type Manager ¶
type Manager struct {
Agents map[string]*Agent
Tasks map[string]*Task
Tools map[string]*Tool
// contains filtered or unexported fields
}
func NewManager ¶
func NewManager() *Manager
func (*Manager) AssignTaskToAgent ¶
func (*Manager) CreateAgent ¶
func (*Manager) CreateTask ¶
func (*Manager) ExecuteAllWorkflows ¶
func (*Manager) ExecuteWorkflow ¶
func (*Manager) InitializeWorkflow ¶
func (m *Manager) InitializeWorkflow(config WorkflowConfig) error
type OpenAIResponse ¶
type Task ¶
type TaskConfig ¶
type WorkflowConfig ¶
type WorkflowConfig struct {
Tasks []TaskConfig
Agents []AgentConfig
}
Click to show internal directories.
Click to hide internal directories.