我目前正在致力于将 AI 支持的 API 集成到我的 React Native 应用程序中,但我在生成文本时遇到了问题。具体来说,我通过 Google Cloud Platform 使用 Google 的生成语言 API (Gemini) 来实现此功能。我已使用 Google Auth 设置身份验证,并且可以成功进行 API 调用,但文本生成功能无法按预期工作。我正在寻求解决此问题的帮助,以便 API 可以按预期在我的应用程序中生成和显示文本。
所以我尝试执行一个curl命令来测试API:
curl -X POST "https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText" \ -H "Authorization: Bearer {My Key}" \ -H "Content-Type: application/json" \ -d '{"prompt": {"text": "Write a humorous caption for a funny cat meme."}}'
但是我遇到了这种错误:
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 411 (Length Required)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>411.</b> <ins>That’s an error.</ins>
<p>POST requests require a <code>Content-length</code> header. <ins>That’s all we know.</ins>
curl: (3) URL rejected: Bad hostname
curl: (3) URL rejected: Malformed input to a URL function
curl: (3) URL rejected: Bad hostname
curl: (3) URL rejected: Malformed input to a URL function
curl: (3) URL rejected: Bad hostname
curl: (3) URL rejected: Malformed input to a URL function
curl: (3) URL rejected: Bad hostname
curl: (3) unmatched brace in URL position 2:
'{prompt:
^
显然,这似乎是由于我的 URL,但我不知道如何为我的 API 获取正确的 URL...
API 密钥不是不记名令牌。 你应该内联传递它。
API_KEY="YOUR_API_KEY"
# Adjust safety settings in generationConfig below.
# See https://ai.google.dev/gemini-api/docs/safety-settings
curl \
-X POST https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${API_KEY} \
-H 'Content-Type: application/json' \
-d @<(echo '{
"contents": [
{
"role": "user",
"parts": [
{
"text": "input: "
},
{
"text": "output: "
}
]
}
],
"generationConfig": {
"temperature": 1,
"topK": 64,
"topP": 0.95,
"maxOutputTokens": 8192,
"responseMimeType": "text/plain"
}
}')