File size: 1,539 Bytes
42b59e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ebb0a5
42b59e5
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
import gradio as gr
from flask import jsonify, request
import pandas as pd
from langchain.utilities import SerpAPIWrapper
from langchain.agents import Tool
from langchain.memory import ConversationBufferMemory
from langchain.llms import Cohere
from langchain.agents import initialize_agent
from langchain.agents.agent_types import AgentType

COHERE_API_KEY = "CggzsdnWH6QXtnGJvKYe4IRZyGZ8UkTSykpmAigW"
SERPAPI_API_KEY = "dbc53dd88c7b0957548a81fa162e2d547e03cc19267162a9166e52d4e882f361"


def get_chat_output(prompt: str):
    try:
        search = SerpAPIWrapper(serpapi_api_key=SERPAPI_API_KEY)
        tools = [
            Tool(
                name="Current search",
                func=search.run,
                description="useful for when you need to answer questions about current events or the current state of the world",
            ),
        ]
        memory = ConversationBufferMemory(memory_key="chat_history")
        input = prompt 
        llm = Cohere(cohere_api_key=COHERE_API_KEY, model="command-xlarge-nightly")
        agent_chain = initialize_agent(
            tools,
            llm,
            agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
            verbose=True,
            memory=memory,
            handle_parsing_errors=True,
        )
        response = agent_chain.run(input=input)
        return {"response": response}

    except:
        return jsonify({"error": "Facing errors, please try again..."})


iface = gr.Interface(fn=get_chat_output, inputs="text", outputs="json")
iface.launch()