Генератор статей на основе PaLM API не работает
Я создал генератор статей среднего размера с использованием PaLM API, но он не работает. Пытались отладить его за последние 2 дня, но он все еще не работает. Я думаю, что ошибка кроется в этой части кода. Помогите мне отладить это. Та часть, в которой, я думаю, кроется ошибка.[Сообщение об ошибке.](https://stackru.com/images/72aa452cb938da3f354a22bc69efe41d31854fd3.png)
import streamlit as st
import requests
import json
PALM_API_ENDPOINT = "https://api.palm.ai/v1/generate"
PALM_API_KEY = "AIzaSyB5ol5ILDVJ-mmi-VBV5EYUD0918__WGrI"
def generate_medium_article(topic):
"""Generates a Medium article on the given topic using the PaLM API."""
payload = {
"prompt": """Write a Medium article on the topic {}.""".format(topic),
"temperature": 0.7,
"max_tokens": 2000,
}
headers = {
"Authorization": "Bearer {}".format(PALM_API_KEY)
}
response = requests.post(PALM_API_ENDPOINT, json=payload, headers=headers)
response.raise_for_status()
article = json.loads(response.content)["generated_text"]
return article
def generate_hashtags(topic):
"""Generates hashtags for the given topic using the PaLM API."""
payload = {
"prompt": """Generate hashtags for the topic {}.""".format(topic),
"temperature": 0.7,
"max_tokens": 5,
}
headers = {
"Authorization": "Bearer {}".format(PALM_API_KEY),
"Accept": "*/*"
}
response = requests.post(PALM_API_ENDPOINT, json=payload, headers=headers)
response.raise_for_status()
hashtags = json.loads(response.content)["generated_text"].split(",")
return hashtags
def main():
"""StreamLit app for generating Medium articles and hashtags."""
st.title("Medium Article Generator and Hashtag Generator")
topic = st.text_input("Enter a topic:")
if st.button("Generate Medium article"):
article = generate_medium_article(topic)
st.write(article)
if st.button("Generate hashtags"):
hashtags = generate_hashtags(topic)
st.write(hashtags)
if __name__ == "__main__":
main()
1 ответ
У вас есть несколько проблем
Во-первых, ваша конечная точка неверна.
PALM_API_ENDPOINT = "https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText"
Во-вторых, неправильный формат полезной нагрузки.
payload = {
'model': 'models/text-bison-001',
"prompt": {
"text": 'Generate hashtags for the topic "' + topic + "'"
},
"temperature": 1.0,
"candidateCount": 1}
В-третьих, ключ API НЕ является токеном на предъявителя, поэтому его не следует отправлять в качестве заголовка авторизации.
headers = {
"contentType": "'application/json'"
}
response = requests.post(PALM_API_ENDPOINT + "?key=" + PALM_API_KEY, json=payload, headers=headers)