|
import gradio as gr |
|
from langchain import hub |
|
from langchain_google_genai import ChatGoogleGenerativeAI |
|
from langchain_core.messages import SystemMessage |
|
from langchain.agents import create_react_agent, AgentExecutor |
|
from langchain_community.agent_toolkits.load_tools import load_tools |
|
from dotenv import find_dotenv, load_dotenv |
|
import os |
|
|
|
load_dotenv(find_dotenv()) |
|
google_api_key = os.environ['GOOGLE_AI_API_KEY'] |
|
|
|
prompt = hub.pull("hwchase17/react-chat") |
|
|
|
template = """ |
|
You are Codora, an AI assistant that helps the user with his/her code. |
|
You accept user input and provide assistance in coding. DO NOT PROVIDE ANSWERS, ONLY GUIDE THEM TO THE SOLUTION. |
|
Provide any necessary information to the user to help them understand the problem and solve it on their own. |
|
Your answers should be crisp, clear and concise. |
|
Use the provided tool to search Stack Overflow for relevant information. |
|
Answer questions that are only related to coding/programming. |
|
Don't respond to requests that are irrelevant to coding/programming. |
|
Politely decline any irrelevant questions and remind them that you need a coding/programming request. |
|
""" |
|
|
|
messages = [SystemMessage(content=template)] |
|
|
|
model = ChatGoogleGenerativeAI(model="gemini-1.5-pro", |
|
google_api_key=google_api_key, |
|
temperature=0.4) |
|
|
|
tools = load_tools(["stackexchange"]) |
|
|
|
agent = create_react_agent(llm=model, |
|
prompt=prompt, |
|
tools=tools) |
|
|
|
agent_executor = AgentExecutor(agent=agent, |
|
verbose=True, |
|
tools=tools) |
|
|
|
def respond(message, history): |
|
messages.append(message) |
|
response = agent_executor.invoke({"input": messages, "chat_history" : history}) |
|
return response['output'] |
|
|
|
demo = gr.ChatInterface( |
|
respond, |
|
examples=["How do I use async/await in a JavaScript function?", "How do I center a div in CSS?", "How do I create a virtual environment in Python?"] |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |