Adjust prompt, improvements

This commit is contained in:
Tanner Collin 2024-08-20 18:44:07 +00:00
parent 30654c1580
commit 44902513fd

29
main.py
View File

@ -21,20 +21,21 @@ TIMEZONE_CALGARY = pytz.timezone('America/Edmonton')
bot = TelegramClient('data/bot', secrets.API_ID, secrets.API_HASH).start(bot_token=secrets.API_TOKEN) bot = TelegramClient('data/bot', secrets.API_ID, secrets.API_HASH).start(bot_token=secrets.API_TOKEN)
CHATGPT_TEMPLATE = ''' CHATGPT_TEMPLATE = '''
Turn this forum post into an two paragraph instagram caption that tells about what a member of our makerspace has made. Turn this forum post into an two paragraph summary that tells about what a member of our makerspace has made.
Add the hashtags: #makerspace #yyc #maker #diy #calgary and several relevant to the post at the end. Add the hashtags: #makerspace #yyc #maker #diy #calgary and several relevant to the post at the end.
Include a sentence explaining that this was made at Calgary Protospace, a makerspace that's non-profit and volunteer run. Include one sentence explaining that this was made at Calgary Protospace, a makerspace that's non-profit and volunteer run.
Only say the member's name once. Only mention the member's name once.
Use no more than 1000 characters. Keep it short, use no more than 1000 characters.
Write in third person. Write in third person.
Do not @ tag anybody. Use neutral sounding phrases.
Title: {} Title: {}
Member: {} Member: {}
Post Body: Post Body:
``` ```
{} {}
```''' ```
'''
try: try:
data = json.load(open('data/data.json')) data = json.load(open('data/data.json'))
@ -116,8 +117,8 @@ def api_chatgpt(prompt):
data = dict( data = dict(
messages=thread, messages=thread,
model='gpt-4-1106-preview', model='gpt-3.5-turbo', #'gpt-4-1106-preview',
temperature=0.5, temperature=0.8,
user='protogram', user='protogram',
max_tokens=1000, max_tokens=1000,
) )
@ -153,7 +154,7 @@ def get_portal_name_from_discourse(username):
logging.error('Problem with getting member name: {} - {}'.format(e.__class__.__name__, str(e))) logging.error('Problem with getting member name: {} - {}'.format(e.__class__.__name__, str(e)))
return False return False
def generate_caption(topic): def generate_caption(topic, prev_captions='N/A'):
title = topic['title'] title = topic['title']
username = topic['post_stream']['posts'][0]['username'] username = topic['post_stream']['posts'][0]['username']
@ -169,6 +170,7 @@ def generate_caption(topic):
filtered_lines = [line for line in lines if 'KB' not in line and 'MB' not in line] filtered_lines = [line for line in lines if 'KB' not in line and 'MB' not in line]
body = '\n'.join(filtered_lines).replace('\n\n\n', '\n\n') body = '\n'.join(filtered_lines).replace('\n\n\n', '\n\n')
#prompt = CHATGPT_TEMPLATE.format(title, member, body, prev_captions)
prompt = CHATGPT_TEMPLATE.format(title, member, body) prompt = CHATGPT_TEMPLATE.format(title, member, body)
logging.info('Generating caption for: {}\n{}'.format(title, body)) logging.info('Generating caption for: {}\n{}'.format(title, body))
@ -265,6 +267,13 @@ async def process_topics():
logging.info('Next valid topic ID: {}'.format(topic_id)) logging.info('Next valid topic ID: {}'.format(topic_id))
try:
prev_states = list(data['states'].values())[-5:]
prev_captions = '\n\n'.join([x['caption'] for x in prev_states])
except:
prev_captions = 'N/A'
logging.info('Previous captions:\n{}\n\n'.format(prev_captions))
if topic_id not in data['states']: if topic_id not in data['states']:
data['states'][topic_id] = dict( data['states'][topic_id] = dict(
status='NEW', status='NEW',
@ -282,7 +291,7 @@ async def process_topics():
store_data() store_data()
continue continue
caption = generate_caption(topic) caption = generate_caption(topic, prev_captions)
state['caption'] = caption state['caption'] = caption
result = await send_data_to_admin(state) result = await send_data_to_admin(state)