import os import openai import gradio as gr import random import time import json import ast def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, # this is the degree of randomness of the model's output ) return response.choices[0].message["content"] def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0): response = openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, # this is the degree of randomness of the model's output ) # print(str(response.choices[0].message)) return response.choices[0].message["content"] def collect_messages(prompt, context): context.append({'role':'user', 'content':f"{prompt}"}) response = get_completion_from_messages(context) context.append({'role':'assistant', 'content':f"{response}"}) return response, context DIVAR_CONTEXT = """ فرض کن ربات دستیار فروش برای اپلیکیشن دیوار هستی که کاربر را کمک کنی. در هر مرحله باید از کاربر سوالاتی بپرسی که نیاز کاربر مشخص شود و در نهایت محصول نهایی را پیشنهاد دهی. در هر مرحله باید از کاربر سوالی بپرسی و فقط آخر مکالمه یک کالای مشخص را پیشنهاد دهی. اپلیکیشن و سایت دیوار، بستری برای انتشار رایگان انواع آگهی و نیازمندی خرید و فروش کالاهای نو و دست‌دوم است. می توان از اپلیکیشن دیوار برای دریافت و ارائه خدمات و همچنین، خرید و فروش کالا در دسته‌بندی‌‌های مختلف زیر استفاده کنید: املاک: خرید و فروش خانه، آپارتمان، ویلا و مغازه وسایل نقلیه : خرید و فروش خودروی سواری، کلاسیک و سنگین و آگهی‌های خودروهای اجاره‌ای لوازم الکترونیکی : خرید و فروش موبایل و تبلت، لپ‌تاپ و رایانه، کنسول بازی، لوازم صوتی و تصویری و ... وسایل منزل : خرید و فروش وسایل خانه، لوازم آشپزخانه، ابزار و وسایل ساختمان و ... خدمات : انواع خدمات پذیرایی و مراسم، نظافت، سرگرمی، رایانه‌ای، مالی و حسابداری و ... وسایل شخصی : خرید و فروش کیف، کفش، لباس، آرایشی و بهداشتی، لباس بچه، لوازم التحریر و ... سرگرمی و فراغت : خرید و فروش بلیط و تور، کتاب و مجله، کلکسیون، آلات موسیقی، لوازم ورزشی و ... اجتماعی : رویداد، رویدادهای داوطلبانه، گم‌شده‌ها و ... برای کسب‌وکار : خرید و فروش تجهیزات و ماشین‌آلات، عمده‌فروشی و ... استخدام و کاریابی : اداری و مدیریتی، سرایداری، عمران و ساختمانی، رایانه و فناوری اطلاعات، مالی و حسابداری و ... شما باید کوتاه و بسیار دوستانه پاسخ دهید. شما باید ویژگی های کالای مورد نظر کاربر را به بهترین شکل از او بپرسید و در نهایت بهترین متن جستجو را خروجی دهید تا در دیوار جستجو شود. Each answer from the bot must have two parts separated by "---". The first part is the response to the user. The second part is a json. In the second part create a short json based on the previous conversation (Make sure to only output the json and nothing more). The fields should be 1) search_query: make it a list of possible specific queries which can find the best items for the user in Divar website in Farsi based on the conversation. If there are absolutely none put an empty string in the list. 2) price_range: with min and max number In Tomans. If the user has not determined the range put 0. 3) possible_filters: keys must be in english The results of this query will be shown to the user next to the chat by the website and you can refer them to those results if needed. As you do not have the recent information such as prices, you can refer the user for such information to the results of the query that are shown on the left side of their chat. Example: سلام من ربات دستیار دیوار هستم. --- {"search_query": ["131 پراید نو"], "price_range": [0, 100000], "possible_filters": {"brand": "پراید", "model": "131"}} Make sure that every response has these two parts separated by "---" and the json is a valid json. ALL OUTPUTS MUST HAVE THE JSON PART. """ base_context = [ {'role':'system', 'content': DIVAR_CONTEXT} ] # accumulate messages def respond(message, chat_history, api_key, context_box): context = ast.literal_eval(context_box) bot_message = "" try: openai.api_key = api_key bot_message, context = collect_messages(message, context) user_response = bot_message.split("---")[0] query_response = bot_message.split("---")[1] chat_history.append((message, user_response)) # messages = context.copy() # messages.append( # {'role':'system', 'content':'create a short json based on the previous conversation (Make sure to only output the json and nothing more). The fields should be 1) search_query (make it a list of possible queries which can find the best items for the user in Divar website) 2) price_range with min and max number In Tomans 3) possible_filters (keys must be in english)' }, # ) # response = get_completion_from_messages(messages, temperature=0) query = ast.literal_eval(query_response)["search_query"][0] h = f"" except Exception as e: h, query_response = bot_message, str(e) return "", chat_history, h, query_response, context def clear_fn(): return None, base_context.copy() with gr.Blocks(title="DivarGPT") as demo: with gr.Row(): gr.HTML('

DivarGPT v0.3

') with gr.Row(): with gr.Column(scale=1): html = gr.HTML("Results Will be shown here!") htmlj = gr.HTML("Results Will be shown here!") with gr.Column(scale=1): api_key_box = gr.Textbox(label="OpenAI API Key", info="OpenAI API Key", lines=1, value="") chatbot = gr.Chatbot() msg = gr.Textbox(label="Chat") clear = gr.Button("Clear") context_box = gr.Textbox(base_context.copy(), label="Context", visible=False) msg.submit(respond, [msg, chatbot, api_key_box, context_box], [msg, chatbot, html, htmlj, context_box]) clear.click(clear_fn, None, [chatbot, context_box], queue=False) demo.launch()