import openai import gradio as gr from serpapi import GoogleSearch openai.api_key = os.environ.get('MY_KEY') messages = [ { "role": "system", "content": ''' You are Dr. Vivek a professional Safety Standards Expert, Standards professional with a deep understanding of the various standards and their applications in different industries, as well as the processes for developing and revising standards. Standards experts may work in industries such as engineering, manufacturing, or quality assurance, and may be involved in developing, implementing, or auditing compliance with standards. You have great attention to detail, Analytical skills, Critical thinking, Knowledge of regulatory requirements, good Problem-solving capability. '''}, ] def g_search(prompt_): try: params = { "q": prompt_, "location": "Bengaluru, Karnataka, India", "hl": "hi", "gl": "in", "google_domain": "google.co.in", "api_key": os.environ.get('G_KEY') } search = GoogleSearch(params) results = search.get_dict() organic_results = results["organic_results"] snippets = "" counter = 1 for item in organic_results: snippets += str(counter) + ". " + item.get("snippet", "") + '\n' + item['about_this_result']['source']['source_info_link'] + '\n' counter += 1 response = openai.Completion.create( model="text-davinci-003", prompt=f'''following are snippets from google search with these as knowledge base only answer questions and print reference link as well followed by answer. \n\n {snippets}\n\n question-{prompt_}\n\nAnswer-''', temperature=0.49, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0) string_temp = response.choices[0].text return string_temp except: pass def openai_chat(prompt_): if prompt_: messages.append( {"role": "user", "content": prompt_}, ) chat_completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages ) reply = chat_completion.choices[0].message.content if ("Oh!" in reply or "Internet" in reply): res = g_search(prompt_) return res.strip() else: return reply.strip() def saab(input, hist=[]): _user_ = "👤: " _saab_ = "🎖️🧓🏻🎖️: " output = openai_chat(input) hist.append((_user_+input, _saab_+output)) return hist, hist gr.Interface(fn=saab, inputs=["text","state"], outputs=["chatbot","state"], title="SSB Virtual Coaching", description="Ask your doubts to Col. sir! (Dev. Vishwesh V Bhat)").launch(share=True)