-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Authored-by: Ann Vasileva <[email protected]>
- Loading branch information
Showing
11 changed files
with
336 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package yandexgpt | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
const getIAMUrl = "https://iam.api.cloud.yandex.net/iam/v1/tokens" | ||
|
||
// Updates IAM token. | ||
// | ||
// Always call it before creating a request. | ||
// | ||
// If you will use it when API key is specified, method CreateRequest(...) will always use API key. | ||
func (c *YandexGPTClient) GetIAMToken(ctx context.Context) error { | ||
iamRq := YandexIAMRequest{OAuthToken: c.config.OAuthToken} | ||
req, err := c.newRequest(ctx, http.MethodPost, getIAMUrl, iamRq) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var resp YandexIAMResponse | ||
err = c.sendRequest(req, &resp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
//set new IAMToken | ||
c.config.SetIAMToken(resp.IAMToken) | ||
return nil | ||
} |
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,54 @@ | ||
package yandexgpt | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
const completionURL = "https://llm.api.cloud.yandex.net/foundationModels/v1" | ||
|
||
// Get completion from YandexGPT. | ||
// | ||
// If you're using IAM token, make sure to update client's IAM token by calling | ||
// GetIAMToken(iamToken string) method first. | ||
// | ||
// Keep in mind that if for some strange reason you provided API key and IAM token to the client, | ||
// this method will use API key. | ||
func (c *YandexGPTClient) GetCompletion( | ||
ctx context.Context, | ||
request YandexGPTRequest, | ||
) (response YandexGPTResponse, err error) { | ||
|
||
endpoint := completionURL + "/completion" | ||
|
||
req, err := c.newRequest(ctx, http.MethodPost, endpoint, request) | ||
if err != nil { | ||
return | ||
} | ||
err = c.sendRequest(req, &response) | ||
|
||
return | ||
} | ||
|
||
// Get completion from YandexGPT with async method. | ||
// | ||
// If you're using IAM token, make sure to update client's IAM token by calling | ||
// GetIAMToken(iamToken string) method first. | ||
// | ||
// Keep in mind that if for some strange reason you provided API key and IAM token to the client, | ||
// this method will use API key. | ||
func (c *YandexGPTClient) RunCompletionAsync( | ||
ctx context.Context, | ||
request YandexGPTRequest, | ||
) (response YandexCompletionResponse, err error) { | ||
endpoint := completionURL + "/completionAsync" | ||
|
||
req, err := c.newRequest(ctx, http.MethodPost, endpoint, request) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = c.sendRequest(req, &response) | ||
|
||
return | ||
} |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package yandexgpt | ||
|
||
type YandexGPTError struct { | ||
HTTPCode int `json:"httpCode"` | ||
Message string `json:"message"` | ||
HTTPStatus string `json:"httpStatus"` | ||
HTTPCode int `json:"httpCode"` | ||
Message string `json:"message"` | ||
HTTPStatus string `json:"httpStatus"` | ||
Details DetailsResponse `json:"error.details"` | ||
} |
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,45 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/sheeiavellie/go-yandexgpt" | ||
) | ||
|
||
func main() { | ||
client := yandexgpt.NewYandexGPTClientWithOAuthToken("OAUTH_TOKEN") | ||
|
||
// get, update and set iam token | ||
ctx := context.Background() | ||
err := client.GetIAMToken(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
request := yandexgpt.YandexGPTRequest{ | ||
ModelURI: yandexgpt.MakeModelURI("CATALOG_ID", yandexgpt.YandexGPT4ModelLite), | ||
CompletionOptions: yandexgpt.YandexGPTCompletionOptions{ | ||
Stream: false, | ||
Temperature: 0.7, | ||
MaxTokens: 100, | ||
}, | ||
Messages: []yandexgpt.YandexGPTMessage{ | ||
{ | ||
Role: yandexgpt.YandexGPTMessageRoleSystem, | ||
Text: "Every time you get ONE you answer just TWO", | ||
}, | ||
{ | ||
Role: yandexgpt.YandexGPTMessageRoleUser, | ||
Text: "ONE", | ||
}, | ||
}, | ||
} | ||
|
||
response, err := client.GetCompletion(ctx, request) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(response.Result.Alternatives[0].Message.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/sheeiavellie/go-yandexgpt" | ||
) | ||
|
||
func main() { | ||
client := yandexgpt.NewYandexGPTClientWithOAuthToken("OAUTH_TOKEN") | ||
|
||
// get, update and set iam token | ||
ctx := context.Background() | ||
err := client.GetIAMToken(ctx) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
request := yandexgpt.YandexGPTRequest{ | ||
ModelURI: yandexgpt.MakeModelURI("CATALOG_ID", yandexgpt.YandexGPT4ModelLite), | ||
CompletionOptions: yandexgpt.YandexGPTCompletionOptions{ | ||
Stream: false, | ||
Temperature: 0.7, | ||
MaxTokens: 100, | ||
}, | ||
Messages: []yandexgpt.YandexGPTMessage{ | ||
{ | ||
Role: yandexgpt.YandexGPTMessageRoleSystem, | ||
Text: "Every time you get ONE you answer just TWO", | ||
}, | ||
{ | ||
Role: yandexgpt.YandexGPTMessageRoleUser, | ||
Text: "ONE", | ||
}, | ||
}, | ||
} | ||
|
||
response, err := client.RunCompletionAsync(ctx, request) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
isCompleted := false | ||
for !isCompleted { | ||
status, err := client.GetOperationStatus(ctx, response.ID) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if status.Done { | ||
isCompleted = true | ||
fmt.Println("\n Chat answer: \n") | ||
fmt.Println(status.Response.Alternatives[0].Message.Text) | ||
} else { | ||
time.Sleep(5 * time.Second) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,24 @@ | ||
package yandexgpt | ||
|
||
type YandexGPTMessage struct { | ||
Role yandexGPTRole `json:"role"` | ||
Text string `json:"text"` | ||
Role yandexGPTRole `json:"role"` | ||
Text string `json:"text"` | ||
ToolCallList *ToolCallList `json:"toolCallList"` | ||
} | ||
|
||
type YandexToolResultList struct { | ||
ToolResults []YandexFunctionResult `json:"toolResults"` | ||
} | ||
|
||
type YandexFunctionResult struct { | ||
Name string `json:"name"` | ||
Content string `json:"content"` | ||
} | ||
|
||
type ToolCallList struct { | ||
ToolCalls []FunctionCall `json:"toolCalls"` | ||
} | ||
type FunctionCall struct { | ||
Name string `json:"name"` | ||
Arguments any `json:"arguments"` | ||
} |
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,24 @@ | ||
package yandexgpt | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
const operationURL = "https://operation.api.cloud.yandex.net/operations" | ||
|
||
func (c *YandexGPTClient) GetOperationStatus( | ||
ctx context.Context, | ||
operationID string, | ||
) (response OperationResponse, err error) { | ||
endpoint := operationURL + "/" + operationID | ||
|
||
req, err := c.newRequest(ctx, http.MethodGet, endpoint, nil) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = c.sendRequest(req, &response) | ||
|
||
return | ||
} |
Oops, something went wrong.