|
import os |
|
import re |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
from langchain.agents.openai_assistant import OpenAIAssistantRunnable |
|
from langchain.agents import AgentExecutor |
|
|
|
from langchain.schema import HumanMessage, AIMessage |
|
|
|
import gradio |
|
|
|
api_key = os.getenv('OPENAI_API_KEY') |
|
extractor_agent = os.getenv('ASSISTANT_ID_SOLUTION_SPECIFIER_A') |
|
|
|
extractor_llm = OpenAIAssistantRunnable(assistant_id=extractor_agent, api_key=api_key, as_agent=True) |
|
|
|
|
|
def remove_citation(text): |
|
|
|
pattern = r"【\d+†\w+】" |
|
|
|
return re.sub(pattern, "📚", text) |
|
|
|
def predict(message, history): |
|
history_langchain_format = [] |
|
for human, ai in history: |
|
history_langchain_format.append(HumanMessage(content=human)) |
|
history_langchain_format.append(AIMessage(content=ai)) |
|
history_langchain_format.append(HumanMessage(content=message)) |
|
gpt_response = extractor_llm.invoke({"content": message}) |
|
output = gpt_response.return_values["output"] |
|
non_cited_output = remove_citation(output) |
|
return non_cited_output |
|
|
|
|
|
chat = gradio.ChatInterface(predict, title="Solution Specifier A", description="testing for the time being") |
|
chat.launch(share=True) |