Added classes for aimodel and article/article_list, implemented wrapper

to use aimodel to extract keyword from a article's content
This commit is contained in:
2024-01-08 04:14:38 +00:00
parent 9d51bd91a8
commit 3dd7d65ee7
8 changed files with 442 additions and 386 deletions
+23 -30
View File
@@ -1,38 +1,31 @@
import os
import sys
import pprint
import google.generativeai as genai
api_key = os.getenv("GEMINI_API_KEY")
assert api_key is not None
genai.configure(api_key=api_key)
max_500_config = genai.GenerationConfig(max_output_tokens=500)
model = genai.GenerativeModel("gemini-pro", generation_config=max_500_config)
SUMMARY_PROMPT = "Sumarize following content:\n"
SUMMARY_PROMPT_ZH = "总结以下内容:\n"
TRANSLATE_PROMPT = "Translate following context to Chinese:\n"
from aimodel import aimodel, aimodel_wrapper
from article_list import article, article_list
from fetch_exper_data import download_xml, read_xml_from_file, xml_dict_to_article_list
def get_response(text: str, prompt: str = SUMMARY_PROMPT) -> str:
"""
Get response from gemini-pro model
text: content
prompt: prompt will be added ahead the content
"""
response = model.generate_content(prompt + text)
return response.text
def main():
url_root = os.getenv("RSSHUB_ROOT")
assert url_root is not None
api_key = os.getenv("GEMINI_API_KEY")
assert api_key is not None
model = aimodel(api_key)
wrapper = aimodel_wrapper(model)
def get_response_list(articles: list, prompt: str = SUMMARY_PROMPT) -> str:
pass
url = url_root + "/yicai/brief"
filename = "exper/raw/yicai.brief.xml"
download_xml(url=url, path=filename)
xml_dict = read_xml_from_file(filename)
article_list = xml_dict_to_article_list(
xml_dict, ["rss", "channel", "item"], ["title", "description"]
)
for i in range(0, range(0, len(article_list.list))):
article_list.list[i].replace_keywords(
wrapper.content_to_5_keywords(article_list.list[i].get_content(), "ZH")
)
print(str(article_list))
if __name__ == "__main__":
f = open(sys.argv[1], "rb")
c = str(f.read(), "utf-8")
pp = pprint.PrettyPrinter()
pp.pprint(get_response(c, SUMMARY_PROMPT_ZH))
main()