File size: 3,884 Bytes
8c26d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503f870
 
 
 
 
8c26d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import ast
import requests
import json
import gradio as gr
from models import *
from pydantic import BaseModel, Field
from workflow import app_graph
from langchain.output_parsers import PydanticOutputParser
from existing_solution import *

class RouterResponse_1(BaseModel):
    route :list[str]= Field(description=("A list of keys relevant to the user's query"))

class SummaryResponse_1(BaseModel):
    information: str=Field(description=("Condensed information based on the context provided"))
    
route_op_1=PydanticOutputParser(pydantic_object=RouterResponse_1)
summary_op_1=PydanticOutputParser(pydantic_object=SummaryResponse_1)

async def solution_langchain(query,prev_msgs,json_data):

    response=await router_chain_1.ainvoke({"query":query,"previous_messages":prev_msgs,"format_instructions":route_op_1.get_format_instructions()})
    routes=route_op_1.parse(response.content).route
    print(routes)
    if len(routes)!=0:
        result = {key: json_data[key] for key in routes}
        print(result)
        response= await summary_chain_1.ainvoke({"query":query,"data":json.dumps(result),"previous_messages":prev_msgs,
                                "format_instructions":summary_op_1.get_format_instructions()})
        return summary_op_1.parse(response.content).information
    else: return "Nothing"

async def process_inputs(input_string,uploaded_file):

    if uploaded_file is not None:
        try:
            with open(uploaded_file) as f:
                file_content = json.load(f)
        except Exception as e:
            print(e)
    else:
        raise Exception("User data Needed")
    input_list=[]
    inputs = {"query": input_string,"previous_msgs":input_list,"ui_data":file_content,'information':[]}

    extracted_1= await solution_langchain(query=input_string,prev_msgs=input_list,json_data=file_content)
    final_state= await app_graph.ainvoke(inputs)
    extracted_2=final_state['information']

    print("==="*50)
    print("LangChain Solution CHATGPT 1\n", extracted_1)
    print("==="*50)
    print("LangGraph Solution CHATGPT 2\n", extracted_2)
    print("==="*50)
    
    

    url = os.getenv("PERSONALITY_URL") + "/chat"
    message_1 = RESPONSE_PROMPT.format(query=input_string, user_information=extracted_1)
    payload_1 = {
        "message": message_1,
        "personality": 'humanish'
    }

    response_1 = requests.post(url, json=payload_1)
    response_1.raise_for_status()

    url = os.getenv("PERSONALITY_URL") + "/chat"
    message_2= RESPONSE_PROMPT.format(query=input_string, user_information=extracted_2)
    payload_2 = {
        "message": message_2,
        "personality": 'humanish'
    }

    response_2 = requests.post(url, json=payload_2)
    response_2.raise_for_status()

    messages = [
        ChatMessage(role="user", content=input_string),]

    # Create a ChatRequest object
    request = ChatRequest(
        messages=messages,
        user_preferences=file_content,
        personality="humanish"
    )

    # Call the chat endpoint asynchronously
    response_3= await chat_endpoint(request)
    
    
    return response_1.json()["response"], response_2.json()["response"], response_3.response


interface = gr.Interface(
    fn=process_inputs,
    inputs=[
        gr.Textbox(label="Enter a string"),   
        gr.File(label="Upload a JSON file", type="filepath")
    ],
    outputs=[
        gr.Textbox(label="Solution 1 Langchain"),  
        gr.Textbox(label="Solution 2 Langgraph"),
        gr.Textbox(label="Existing Solution"),  
    ],
    title="Extracting Relevant UI",
    description="Provide a query, previous messages and user_data. Make sure in user data these keys are present :['name', 'age', 'gender', 'preferences', 'personalInformation', 'relatedDemographics', 'history', 'painPoints', 'inefficienciesOrQualityOfLifeImprovements', 'additionalNotes']"
)

interface.launch()