import gradio as gr import openai import requests import json import os from duckduckgo_search import ddg import datetime from datetime import datetime, date, time, timedelta def search_duckduckgo(query): keywords = query results = ddg(keywords, region='wt-wt', safesearch='Off', time='m') filtered_results = [{"title": res["title"], "body": res["body"]} for res in results] print(filtered_results) return filtered_results def get_search_query(task): response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content": f"Given the task: {task}. Generate a concise search query with 1-3 keywords."} ] ) search_query = response.choices[0]['message']['content'].strip() print("Agent 2: ",search_query) return search_query def summarize_search_result(task, search_result): response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content":f"Given the task '{task}' and the search result '{json.dumps(search_result)}', provide a summarized result."} ] ) summary = response.choices[0]['message']['content'].strip() return summary def agent_1(objective): response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content":f"Given the objective '{objective}', create a list of tasks that are closely related to the objective. If needed add specific key words from objective to the task sentence"} ] ) tasks = response.choices[0]['message']['content'].strip().split('\n') return tasks def agent_2(task): search_query = get_search_query(task) print("Agent 2") print(search_query) search_results = search_duckduckgo(search_query) summarized_result = summarize_search_result(task, search_results) print(summarized_result) return summarized_result def agent_3(objective, last_result, tasks): task_list = '\n'.join(tasks) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content":f"Given the objective '{objective}', the last result '{json.dumps(last_result)}', and the task list:\n{task_list}\n\nRank the tasks based on their relevance to the objective."} ] ) modified_tasks = response.choices[0]['message']['content'].strip().split('\n') print("Agent 3") print(modified_tasks) return modified_tasks def summarize_result(objective, result): response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content":f"Given the objective '{objective}' and the final result '{json.dumps(result)}', provide a summary."} ] ) summary = response.choices[0]['message']['content'].strip() return summary def generate_final_answer(objective, all_results): response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content":f"Given the objective '{objective}' and the collected results '{json.dumps(all_results)}', provide a final answer addressing the objective."} ] ) final_answer = response.choices[0]['message']['content'].strip() return final_answer def main(objective, loop_count): tasks = agent_1(objective) all_results = [] completed_tasks = [] for i in range(loop_count): print(i+1) if i < len(tasks): print('NEXT TASK: ',tasks[i]) completed_tasks.append(tasks[i]) result = agent_2(tasks[i]) all_results.append(result) tasks = agent_3(objective, result, tasks) print('*********************') else: break final_answer = generate_final_answer(objective, all_results) return final_answer,completed_tasks,tasks def getbabyagianswer(objective,loop_count,openapikey): dateforfilesave=datetime.today().strftime("%d-%m-%Y %I:%M%p") print(objective) print(dateforfilesave) if openapikey=='': return ["Please provide OpenAPI Key","Please provide OpenAPI Key","Please provide OpenAPI Key"] os.environ['OPENAI_API_KEY'] = str(openapikey) openai.api_key=str(openapikey) final_summary,completed_tasts,all_tasks = main(objective, int(loop_count)) print("Final Summary:", final_summary) return final_summary,completed_tasts,all_tasks with gr.Blocks() as demo: gr.Markdown("