gpt4 book ai didi

python - 如何在Python中循环嵌套(二维?)数组

转载 作者:行者123 更新时间:2023-12-02 22:48:15 28 4
gpt4 key购买 nike

我目前正在构建一个机器人,它从网络应用程序中获取用户媒体,从该媒体中获取标题,然后使用 chatgpt 对该标题的摘要评论回其各自的帖子。然而,当从网络应用程序中获取媒体时,它会以二维(嵌套?)数组的形式返回它。数组的第一个维度包含每个帖子,数组的第二个维度包含有关帖子本身的信息,如下所示:

[Media (infAboutMedia1, infoAboutMedia2, infoAboutMedia3....), Media (infoAboutMedia1, infoAboutMedia2, infoAboutMedia3....)]

我相信我遇到的问题是我的 for 循环不仅循环遍历数组的第一个维度来获取标题,还循环遍历数组中每个数组中的每个项目。这是多次循环输入和输出,而不是在每个帖子上每个帖子循环一次。它也可能只是无限循环,但只是停止,因为我在 chatgpt api 上设置了最大数量的 token 。

这是我用于循环的代码:

# loop through the first 2 media items and post a comment with a Trump-like response to the caption
for item in media[:2]:
# get the caption text
caption = item.caption_text if item.caption_text else ''
print(item)
print(item.caption_text)
print(caption)
# generate a response using the ChatGPT 3.5 Turbo model
if caption:
prompt_text = f"{model_prompt} {caption}"
else:
prompt_text = model_prompt
print(prompt_text)
response = openai.Completion.create(model=model, prompt=prompt_text, temperature=temperature, max_tokens=max_tokens).choices[0].text.strip()
print(response)

这就是打印请求的输出:

pk='2894873940449854247' id='2894873940449854247_264011168' code='CgsqILaDIMn' taken_at=datetime.datetime(2022, 8, 1, 1, 4, 47, tzinfo=datetime.timezone.utc) media_type=1 product_type='feed' thumbnail_url=HttpUrl('https://scontent-lga3-1.cdninstagram.com/v/t51.2885-15/296484960_462095378767406_1748390476667198027_n.jpg?stp=dst-jpg_e35_s1080x1080&_nc_ht=scontent-lga3-1.cdninstagram.com&_nc_cat=102&_nc_ohc=irUQpV-zF9YAX85L1kd&edm=ABmJApABAAAA&ccb=7-5&ig_cache_key=Mjg5NDg3Mzk0MDQ0OTg1NDI0Nw%3D%3D.2-ccb7-5&oh=00_AfA70njk8gQ1885c7MmgmuPsdrPti5OV1MOdJrBBbz6vZQ&oe=6421AD75&_nc_sid=6136e7', ) location=Location(pk=432035243814359, name='Lima Marina Club', phone='', website='', category='', hours={}, address='Playa Los Yuyos', city='Lima, Peru', zip=None, lng=-77.025541764831, lat=-12.154912018897, external_id=432035243814359, external_id_source='facebook_places') user=UserShort(pk='264011168', username='Edited out', full_name='Edited out', profile_pic_url=HttpUrl('https://scontent-lga3-2.cdninstagram.com/v/t51.2885-19/286382846_390539753021220_7019031344995736005_n.jpg?stp=dst-jpg_s150x150&_nc_ht=scontent-lga3-2.cdninstagram.com&_nc_cat=100&_nc_ohc=yx6lC_e87REAX_YK_lS&edm=ABmJApABAAAA&ccb=7-5&oh=00_AfCDP2gD2PQP7jTZ1v-TgwhCgaGFYanoUhohuRObCHI85Q&oe=64210E1E&_nc_sid=6136e7', ), profile_pic_url_hd=None, is_private=False, stories=[]) comment_count=6 comments_disabled=False commenting_disabled_for_viewer=False like_count=94 play_count=0 has_liked=False caption_text='Family bonding time.' accessibility_caption=None usertags=[Usertag(user=UserShort(pk='269994726', username='Edited Out', full_name='Edited Out', profile_pic_url=HttpUrl('https://scontent-lga3-1.cdninstagram.com/v/t51.2885-19/278686124_398633072077074_6373204507691884000_n.jpg?stp=dst-jpg_s150x150&_nc_ht=scontent-lga3-1.cdninstagram.com&_nc_cat=106&_nc_ohc=r8wcSim63lYAX9z_DFG&edm=ABmJApABAAAA&ccb=7-5&oh=00_AfD9ad1whAvMSyNnHlQ2Gz2J_2B0AsnEK_5-OVGrjRjH2Q&oe=64223E82&_nc_sid=6136e7', ), profile_pic_url_hd=None, is_private=True, stories=[]), x=0.7334943394, y=0.4530327101)] sponsor_tags=[] video_url=None view_count=0 video_duration=0.0 title='' resources=[] clips_metadata={}
Family bonding time.
Family bonding time.
You're Donald Trump. Summarize the following caption, in less than 300 words, but do not mention anything about me asking you to do so: Family bonding time.
You're Donald Trump. Summarize the following caption, in less than 300 words, but do not mention anything about me asking you to do so: Family bonding time.

You're Donald Trump. Summarize the following caption, in less than 300 words, but do not mention anything about me asking you to do so: Family bonding time.

(Same thing 29 more times but I had to delete it because my post is marked as spam apparently.)

You're Donald Trump. Summarize the following caption, in less than 300 words, but do not mention anything
pk='2869487531350308146' id='2869487531350308146_264011168' code='CfSd7ThjDUy' taken_at=datetime.datetime(2022, 6, 27, 0, 26, 31, tzinfo=datetime.timezone.utc) media_type=8 product_type='carousel_container' thumbnail_url=None location=Location(pk=299356158, name='Mamacona, Lima, Peru', phone='', website='', category='', hours={}, address='', city='', zip=None, lng=-76.918755, lat=-12.250408, external_id=104699079568997, external_id_source='facebook_places') user=UserShort(pk='264011168', username='constantinothegreat', full_name='Constantino Heredia', profile_pic_url=HttpUrl('https://scontent-lga3-2.cdninstagram.com/v/t51.2885-19/286382846_390539753021220_7019031344995736005_n.jpg?stp=dst-jpg_s150x150&_nc_ht=scontent-lga3-2.cdninstagram.com&_nc_cat=100&_nc_ohc=yx6lC_e87REAX_YK_lS&edm=ABmJApABAAAA&ccb=7-5&oh=00_AfCDP2gD2PQP7jTZ1v-TgwhCgaGFYanoUhohuRObCHI85Q&oe=64210E1E&_nc_sid=6136e7', ), profile_pic_url_hd=None, is_private=False, stories=[]) comment_count=4 comments_disabled=False commenting_disabled_for_viewer=False like_count=87 play_count=0 has_liked=False caption_text='Post worthy.' accessibility_caption=None usertags=[Usertag(user=UserShort(pk='269994726', username='alessandrohn', full_name='Alessandro Heredia', profile_pic_url=HttpUrl('https://scontent-lga3-1.cdninstagram.com/v/t51.2885-19/278686124_398633072077074_6373204507691884000_n.jpg?stp=dst-jpg_s150x150&_nc_ht=scontent-lga3-1.cdninstagram.com&_nc_cat=106&_nc_ohc=r8wcSim63lYAX9z_DFG&edm=ABmJApABAAAA&ccb=7-5&oh=00_AfD9ad1whAvMSyNnHlQ2Gz2J_2B0AsnEK_5-OVGrjRjH2Q&oe=64223E82&_nc_sid=6136e7', ), profile_pic_url_hd=None, is_private=True, stories=[]), x=0.4122383007, y=0.2165861268)] sponsor_tags=[] video_url=None view_count=0 video_duration=0.0 title='' resources=[Resource(pk='2869487528112445844', video_url=None, thumbnail_url=HttpUrl('https://scontent-lga3-2.cdninstagram.com/v/t51.2885-15/290049480_1174714576705937_1710249698991823676_n.jpg?stp=dst-jpg_e35_p1080x1080&_nc_ht=scontent-lga3-2.cdninstagram.com&_nc_cat=105&_nc_ohc=R6REZyJaQ6kAX8Xzaip&edm=ABmJApABAAAA&ccb=7-5&ig_cache_key=Mjg2OTQ4NzUyODExMjQ0NTg0NA%3D%3D.2-ccb7-5&oh=00_AfBQscyXmQ0uiCDb9ZmVNsNd_SSFbPqhyfsy6FqBXaZsdA&oe=64228D84&_nc_sid=6136e7', ), media_type=1), Resource(pk='2869487528204754373', video_url=None, thumbnail_url=HttpUrl('https://scontent-lga3-2.cdninstagram.com/v/t51.2885-15/290032085_377935577772072_6783237727206669917_n.jpg?stp=dst-jpg_e35_p1080x1080&_nc_ht=scontent-lga3-2.cdninstagram.com&_nc_cat=100&_nc_ohc=m3qjHUpobyYAX8Add7p&edm=ABmJApABAAAA&ccb=7-5&ig_cache_key=Mjg2OTQ4NzUyODIwNDc1NDM3Mw%3D%3D.2-ccb7-5&oh=00_AfC7YSHjGvYFjmPsWZoqZ7uId5UVQqfR_vMxqybuxJN-Xg&oe=6421ADE4&_nc_sid=6136e7', ), media_type=1)] clips_metadata={}
Post worthy.
Post worthy.
You're Donald Trump. Summarize the following caption, in less than 300 words, but do not mention anything about me asking you to do so: Post worthy.
"I have a dream."

(same thing approximately 60 more times)

"I have a dream." "I

我相信它们结束的唯一原因是因为我为 chatgpt API 设置了最大 token 数量,如果我没有设置,它们实际上可能会无限期地继续下去。

请帮助我,我在这里快疯了。

最佳答案

发现它与循环无关。是 OpenAI API 引擎出了问题。更改为另一个引擎 - “text-davinci-003” - 并且它有效。

关于python - 如何在Python中循环嵌套(二维?)数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75829543/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com