-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package summaraizer | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// Google is a provider that uses Google as an AI provider. | ||
type Google struct { | ||
Model string // The Ai model to use. | ||
Prompt string // The prompt to use for the AI model. | ||
ApiToken string // The API Token for Google. | ||
} | ||
|
||
func (a *Google) Summarize(reader io.Reader) (string, error) { | ||
return decodeAndSummarize(reader, func(comments Comments) (string, error) { | ||
prompt, err := resolvePrompt(a.Prompt, comments) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
request := googleRequest{ | ||
GoogleContentsRequest: []googleContentsRequest{ | ||
{ | ||
Parts: []googlePartsRequest{ | ||
{ | ||
Text: prompt, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
reqBodyBytes, err := json.Marshal(request) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
url := fmt.Sprintf( | ||
"https://generativelanguage.googleapis.com/v1beta/models/%s:generateContent?key=%s", | ||
a.Model, | ||
a.ApiToken, | ||
) | ||
req, err := http.NewRequest( | ||
"POST", | ||
url, | ||
bytes.NewBuffer(reqBodyBytes), | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
respBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
var response googleResponse | ||
err = json.Unmarshal(respBody, &response) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return response.Candidates[0].Content.Parts[0].Text, nil | ||
}) | ||
} | ||
|
||
type googleRequest struct { | ||
GoogleContentsRequest []googleContentsRequest `json:"contents"` | ||
} | ||
|
||
type googleContentsRequest struct { | ||
Parts []googlePartsRequest `json:"parts"` | ||
} | ||
|
||
type googlePartsRequest struct { | ||
Text string `json:"text"` | ||
} | ||
|
||
type googleResponse struct { | ||
Candidates []struct { | ||
Content struct { | ||
Parts []struct { | ||
Text string `json:"text"` | ||
} `json:"parts"` | ||
} `json:"content"` | ||
} `json:"candidates"` | ||
} |
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,25 @@ | ||
## summaraizer google | ||
|
||
Summarizes using Google AI | ||
|
||
### Synopsis | ||
|
||
Summarizes using Google AI. | ||
|
||
``` | ||
summaraizer google [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for google | ||
--model string The AI model to use (default "gemini-1.5-flash-8b") | ||
--prompt string The prompt to use for the AI model (default "\nI give you a discussion and you give me a summary.\nEach comment of the discussion is wrapped in a <comment> tag.\nYour summary should not be longer than 1200 chars.\nHere is the discussion:\n{{ range $comment := . }}\n<comment>{{ $comment.Body }}</comment>\n{{end}}\n") | ||
--token string The API Token for Google | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [summaraizer](summaraizer.md) - Summarizes comments | ||
|