-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from shellfly/feat-summary
feat: summary
- Loading branch information
Showing
7 changed files
with
98 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
) | ||
|
||
func init() { | ||
commands["summary"] = &Summary{} | ||
} | ||
|
||
type Summary struct { | ||
dummyCommand | ||
} | ||
|
||
func (c *Summary) Name() string { | ||
return "summary" | ||
} | ||
|
||
func (c *Summary) Help() string { | ||
return "/summary - generate summary from a URL" | ||
} | ||
|
||
// Prompts expand input like "{lang} {question}" to Summary generation prompts | ||
func (c *Summary) Prompts(input string) []string { | ||
parts := strings.Split(input, " ") | ||
var lang, url string | ||
if len(parts) == 2 { | ||
lang = parts[0] | ||
url = parts[1] | ||
} else { | ||
url = input | ||
} | ||
|
||
content := crawl(url) | ||
prompt := fmt.Sprintf(`Generate a summary of the below text content.n\nText:"""\n%s\n"""`, content) | ||
if language, ok := Languages[lang]; ok { | ||
prompt = prompt + "\nTranslate the response to " + language | ||
} | ||
return []string{prompt} | ||
} | ||
|
||
func crawl(url string) string { | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
fmt.Println("Error fetching URL: ", err) | ||
return "" | ||
} | ||
defer resp.Body.Close() | ||
|
||
doc, err := goquery.NewDocumentFromReader(resp.Body) | ||
if err != nil { | ||
fmt.Println("Error reading response body: ", err) | ||
return "" | ||
} | ||
doc.Find("nav, script, iframe, style, footer").Remove() | ||
r := regexp.MustCompile(`\s+`) | ||
text := doc.Find("body").Text() | ||
text = r.ReplaceAllString(text, " ") | ||
return text | ||
} |
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