danielmiessler / fabric

fabric is an open-source framework for augmenting humans using AI. It provides a modular framework for solving specific problems using a crowdsourced set of AI prompts that can be used anywhere.
https://danielmiessler.com/p/fabric-origin-story
MIT License
23.24k stars 2.46k forks source link

Added Jina AI helper Tool #902

Closed johnconnor-sec closed 1 week ago

johnconnor-sec commented 4 weeks ago

What do you need?

I created a Jina AI helper tool, like the yt tool, to search and pull down public website content. This can be piped to fabric like the yt tool. I am currently working to remove all Markdown links (url, image, etc.) from the search response. This will cut down on token usage and unnecessary data being passed to the LLM.

Jina AI GO Tool

Here is the code for easy reference:

package main

import (
    "flag"
    "fmt"
    "io"
    "net/http"
)

func makeRequest(url string, inputStr string) (string, error) {
    fullURL := url + inputStr
    response, err := http.Get(fullURL)
    if err != nil {
        return "", fmt.Errorf("error making request: %v", err)
    }
    defer response.Body.Close()

    body, err := io.ReadAll(response.Body)
    if err != nil {
        return "", fmt.Errorf("error reading response body: %v", err)
    }

    return string(body), nil
}

func main() {
    // Parse command-line arguments
    readURL := flag.String("r", "", "The URL to be sent to the API")
    searchInput := flag.String("s", "", "The input string to be sent to the API")
    flag.Parse()

    if *readURL == "" && *searchInput == "" {
        fmt.Println("Please provide either -r for read URL or -s for search input")
        return
    }

    var url, input string
    if *readURL != "" {
        url = "https://r.jina.ai/"
        input = *readURL
    } else {
        url = "https://s.jina.ai/"
        input = *searchInput
    }

    // Make the request
    response, err := makeRequest(url, input)
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }

    // Print the response
    fmt.Println(response)
}

And the test:

package main

import (
    "net/http"
    "net/http/httptest"
    "testing"
)

// TestMakeRequest tests the makeRequest function
func TestMakeRequest(t *testing.T) {
    // Create a test server
    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        w.Write([]byte("Hello, client"))
    }))
    defer ts.Close()

    // Test cases
    tests := []struct {
        url      string
        inputStr string
        expected string
    }{
        {ts.URL, "", "Hello, client"},
        {ts.URL, "/test", "Hello, client"},
    }

    for _, test := range tests {
        t.Run(test.url+test.inputStr, func(t *testing.T) {
            response, err := makeRequest(test.url, test.inputStr)
            if err != nil {
                t.Fatalf("Expected no error, got %v", err)
            }
            if response != test.expected {
                t.Fatalf("Expected %v, got %v", test.expected, response)
            }
        })
    }
}
eugeis commented 1 week ago

it is integrated now.