import json import openai import os import pandas as pd import gradio as gr from collections import deque from typing import Dict, List, Optional, Any from langchain import LLMChain, OpenAI, PromptTemplate import datetime from datetime import datetime, date, time, timedelta from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, LLMPredictor, Document, ServiceContext from langchain.llms import OpenAIChat import feedparser import pandas as pd import numpy as np from duckduckgo_search import ddg_videos from duckduckgo_search import ddg def get_learning_curriculum(openapikey,topic): dateforfilesave=datetime.today().strftime("%d-%m-%Y %I:%M%p") print(topic) print(dateforfilesave) if openapikey=='': return pd.DataFrame(["Please provide OpenAPI Key"],columns=['ERROR']) os.environ['OPENAI_API_KEY'] = str(openapikey) ###Task Creation Agent prompt='You are a training center AI. Give me a detailed curriculum to learn about "{topicforquery}" using search. The curriculum will be a series of learning tasks to be achieved. Give output as a python list of jsons with "task name", "search keyword" to search to complete the task. Donot repeat the taks. For each task name also add a list of "questions" to ask the search results data to select specific articles and complete the curriculum. Remember the search list will be a dataframe of titles & body of the searched article and you may not be able to go through the full article hence these questions should be of types "Which article best suits a learning curriculum?", "Which article is learning oriented?. To reiterate output should be in json with keys task name ex: get beginner training articles for painting, search keyword ex: beginner painting & questions ex: What are top articles for painting?'.format(topicforquery=topic) openai.api_key = os.getenv("OPENAI_API_KEY") resp=openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content": prompt} ] ) tasklist=json.loads(resp['choices'][0]['message']['content']) ###Function to search the internet using DuckDuckGo exposed as a tool def research_search(search_keyword,question_to_ask,topic): llm_predictor = LLMPredictor(llm=OpenAIChat(temperature=0, model_name="gpt-3.5-turbo")) service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor) keyword=search_keyword keyword="+".join(keyword.lower().split()) keyword=keyword.replace(' and ',' AND ') posts = ddg(keyword+' '+topic, safesearch='Off', page=1) latestnews_df=pd.DataFrame(posts) print(latestnews_df.columns) #latestnews_df=latestnews_df.drop_duplicates(subset=['title','link','published']) latestnews_df['text']='Title: '+latestnews_df['title']+' Description: '+latestnews_df['body'] print(latestnews_df['text'].tolist()) documents=[Document(t) for t in latestnews_df['text'].tolist()] index = GPTSimpleVectorIndex.from_documents(documents) prompt_query=question_to_ask respstr=str(index.query(prompt_query, service_context=service_context, response_mode="tree_summarize", similarity_top_k=10)) print("Search response: ",respstr) return respstr ###Task Execution Agent loop list1=[] list2=[] list3=[] for i in range(len(tasklist)): taskstuff=tasklist[i] search_keyword=taskstuff['search keyword'] for question in taskstuff['questions']: response_string=research_search(search_keyword,question,topic) list1.append(taskstuff['task name']) list2.append(question) list3.append(response_string) ###Create dataframe to display outputdf=pd.DataFrame() outputdf['Task']=list1 outputdf['Question']=list2 outputdf['Learning']=list3 return outputdf with gr.Blocks() as demo: gr.Markdown("