File size: 7,657 Bytes
f8f41b9
8e967a7
 
 
1d2148a
a72034e
90729fc
8e967a7
90729fc
8e967a7
7a27b40
951b467
a72034e
951b467
474437d
 
 
951b467
8e967a7
90729fc
f8f41b9
951b467
8e967a7
 
 
 
f8f41b9
951b467
8e967a7
 
474437d
 
8e967a7
 
2459b19
 
 
 
a72034e
2459b19
 
 
8e967a7
 
 
474437d
2459b19
474437d
 
8e967a7
 
951b467
8e967a7
90729fc
 
 
 
474437d
90729fc
cbbcc75
 
90729fc
 
 
cbbcc75
 
 
 
90729fc
 
2459b19
474437d
 
 
 
 
 
 
 
2459b19
 
 
 
 
 
 
 
 
 
a72034e
 
 
 
2459b19
405c09f
 
 
 
2459b19
 
8e967a7
e5580aa
2459b19
 
 
405c09f
2459b19
 
 
 
 
a72034e
2459b19
 
 
 
 
7a27b40
 
a72034e
 
 
 
 
2459b19
 
a72034e
2459b19
 
405c09f
 
2459b19
 
 
405c09f
2459b19
 
 
 
 
 
7a27b40
2459b19
 
 
 
 
 
1d2148a
90729fc
2459b19
 
cbbcc75
 
405c09f
 
 
 
 
 
 
 
 
90729fc
7a27b40
8e967a7
 
474437d
2459b19
 
 
 
474437d
a72034e
8ce2b62
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import gradio as gr
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import TextLoader
from agents.tools.python_code_tool import generate_and_excute_python_code
from chains import HumanFeedBackChain, contextRewriteChain
from embedding import CustomEmbedding
from memories import HumenFeedbackBufferMemory
from langchain.memory import ConversationBufferMemory
from promopts import FEEDBACK, FEEDBACK_PROMPT
from agents.code_generate_agent import code_agent_executor, code_agent_tools

# llm = ChatOpenAI(temperature=0.7)

baMemory = HumenFeedbackBufferMemory(
            input_key="input", human_prefix="Answer", ai_prefix="AI")
baChain = HumanFeedBackChain(verbose=True, memory=baMemory)

"""读取document/business_context.py文件内容作为context"""
context_path = "./documents/bussiness_context/business_context.md"


def sendMessage(chatbot, input):
    chatbot.append((
        (None if len(input) == 0 else input), None))
    return chatbot


def clearMemory(chatbot):
    chatbot.clear()
    if baMemory != None:
        baMemory.clear()
    return chatbot, ""

def loadContext():
    textloader = TextLoader(context_path)
    return textloader.load()[0].page_content


def saveContext(context):
    with open(context_path, 'w') as f:
        f.write(context)

def feedBack(context, story, chatbot=[], input=""):
    if len(input) > 0:
        context += (f"\n\n {input}")
        saveContext(context)
    response = baChain.run(
        input=(input if len(input) == 0 else input), context=context, story=story, stop="\nAnswer:")
    chatbot[-1][1] = response
    return chatbot, "", context


customerEmbedding = CustomEmbedding()

faqChain = customerEmbedding.getFAQChain()

code_agent_executor = code_agent_executor()
def faqFromLocal(input, chatbot=[]):
    response = faqChain({"question": f"{input}"})
    chatbot.append((input, response["answer"]))
    return chatbot, ""


def generateEmbeddings(chatbot=[]):
    response = customerEmbedding.calculateEmbedding()
    chatbot.append((None, response))
    return chatbot


def generateCode(input: str, chatbot=[], returnCode=False):
    if len(input) <=0:
        chatbot[-1][1] = None
        return chatbot, ""
    response = code_agent_executor.run(
        input=(input if len(input) == 0 else input))
    chatbot[-1][1] = response
    return chatbot, ""

def generateCodeByMultiPart(context: str, relateCode: str, toolName: str, chatbot=[]):
    input = f"请根据如下信息{toolName}:\n{context}\n\n{relateCode}"
    return generateCode(input, chatbot)

def sendMessageByMultiPart(chatbot, context: str, relateCode: str, toolName: str):
    input = f"请根据如下信息{toolName}:\n{context}\n\n{relateCode}"
    chatbot.append((input, None))
    return chatbot


def rewriteContext(input, chatbot):
    response = contextRewriteChain.run(input=input, verbose=True)
    chatbot.append((input, response))
    return chatbot, response

def generateCodeAndExcute(input, chatbot=[]):
    result = generate_and_excute_python_code.run(input)
    chatbot.append((input, result))
    return chatbot

toolTextBox = []
with gr.Blocks() as demo:
    with gr.Row():
            with gr.Tab("Business"):
                with gr.Row():
                    with gr.Column():
                        chatbot = gr.Chatbot().style()
                        with gr.Row():
                            txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(
                                container=False)
                    with gr.Column():
                        with gr.Row():
                            context = gr.Textbox(show_label=True, label="Context", placeholder="Enter Context").style(
                                container=False)
                        with gr.Row():
                            story = gr.Textbox(show_label=True, label="User Story", placeholder="Enter User Story").style(
                                container=False)
                        with gr.Row():
                            gr.Button("Generate Scenarios").click(clearMemory, [chatbot], [chatbot, txt]).then(sendMessage, [chatbot, txt], [chatbot]).then(
                                feedBack, [context, story, chatbot], [chatbot, txt])
                        with gr.Row():
                            with gr.Column(scale=5):
                                gr.Button("Rewrite Context").click(rewriteContext, [context, chatbot], [chatbot, context])
                            with gr.Column(scale=1):
                                gr.Button("Revert").click(loadContext, [], [context])
                        with gr.Row():
                            gr.Button("Save Context").click(saveContext, [context], [])

            with gr.Tab("Tech"):
                with gr.Row():
                    with gr.Column():
                        code_chatbot = gr.Chatbot().style()
                        with gr.Row():
                            code = gr.Textbox(show_label=False, label="Code Generate", placeholder="Enter text and press enter").style(
                            container=False)
                    with gr.Column():
                        with gr.Row():
                            code_context = gr.Textbox(show_label=True, label="Context", placeholder="Enter Context").style(
                                container=False)
                        with gr.Row():
                            relateCode = gr.Textbox(show_label=True, label="Relate Code", placeholder="Enter Relate Code").style(
                                container=False)
                        for index, tool in enumerate(code_agent_tools):
                            with gr.Row():
                                toolTextBox.append(gr.Textbox(show_label=False, visible=False, label=tool.name, value=tool.name).style())
                                gr.Button(tool.name).click(
                                    sendMessageByMultiPart, [code_chatbot, code_context, relateCode, toolTextBox[index]], [code_chatbot]).then(
                                    generateCodeByMultiPart, [code_context, relateCode, toolTextBox[index], code_chatbot], [code_chatbot, code])
            with gr.Tab("FAQ"):
                faq_chatbot = gr.Chatbot().style()
                with gr.Row():
                    faq = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(
                        container=False)
                with gr.Row():
                    gr.Button("Regenerate embedding").click(generateEmbeddings,[faq_chatbot], [faq_chatbot])
            with gr.Tab("TOOL"):
                with gr.Row():
                    with gr.Column():
                        tool_request = gr.Textbox(show_label=False, placeholder="Enter your tool Request").style(
                            container=False, show_copy_button=True)
                        tool_button = gr.Button("Generate Code and Execute")
                    with gr.Column():
                        tool_chatbot = gr.Chatbot(elem_id="chatbot").style(container=False)
                    tool_button.click(generateCodeAndExcute,[tool_request, tool_chatbot], [tool_chatbot])

        
    txt.submit(sendMessage, [chatbot, txt], [chatbot]).then(
        feedBack, [context, story, chatbot, txt], [chatbot, txt, context])
    
    code.submit(sendMessage, [code_chatbot, code], [code_chatbot]).then(
        generateCode, [code, code_chatbot], [code_chatbot, code])
    
    faq.submit(faqFromLocal, [faq, faq_chatbot], [faq_chatbot, faq])
    
    demo.load(loadContext, [], [context])    
demo.launch()