google/generative-ai-go

Who can help me? How should I set up the proxy?

Closed this issue · 1 comments

"I really want to use this SDK package and don't want to implement the API interface myself anymore. I'm begging the experts, please teach me how to set up the proxy. Thank you."

this's my code

var Proxy = "http://127.0.0.1:7890"

func main() {
	test2(SetProxy())
}

func test2(c *http.Client) {
	ctx := context.Background()
	// Access your API key as an environment variable (see "Set up your API key" above)
	client, err := genai.NewClient(ctx, option.WithHTTPClient(c), option.WithAPIKey("AIzaSyBMwViEE"))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// For text-only input, use the gemini-pro model
	model := client.GenerativeModel("gemini-pro")
	resp, err := model.GenerateContent(ctx, genai.Text("你好"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp)
}

// SetAiClientSteam 设置ai 代理
func SetProxy() *http.Client {
	var transport *http.Transport
	if Proxy != "" {
		proxyURL, err := url.Parse(Proxy)
		if err != nil {
			fmt.Println("出现错误:" + err.Error())
			return nil
		}
		transport = &http.Transport{
			Proxy: http.ProxyURL(proxyURL),
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
		}
	} else {
		transport = &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
		}
	}
	client := &http.Client{Transport: transport}
	return client
}

the response is
2023/12/17 21:19:52 googleapi: Error 403: Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API. exit status 1

and i can get response by postman (use proxy)
image

This is a duplicate of #17
Please use that issue for this topic - no need to create new ones. If you have additional information, add it in #17