Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from langchain_core.output_parsers import StrOutputParser
|
4 |
+
from langchain_core.prompts import PromptTemplate
|
5 |
+
from langchain_openai import ChatOpenAI
|
6 |
+
|
7 |
+
def create_chains(openai_key):
|
8 |
+
os.environ["OPENAI_API_KEY"] = openai_key
|
9 |
+
|
10 |
+
# Create the classifier chain
|
11 |
+
classifier_prompt = PromptTemplate.from_template(
|
12 |
+
"""Given the user question below, classify it as either being about `LangChain`, `OpenAI`, or `Other`.
|
13 |
+
Do not respond with more than one word.
|
14 |
+
|
15 |
+
<question>
|
16 |
+
{question}
|
17 |
+
</question>
|
18 |
+
|
19 |
+
Classification:"""
|
20 |
+
)
|
21 |
+
|
22 |
+
classifier_chain = classifier_prompt | ChatOpenAI(model="gpt-4") | StrOutputParser()
|
23 |
+
|
24 |
+
# Create specialized chains
|
25 |
+
langchain_chain = (
|
26 |
+
PromptTemplate.from_template(
|
27 |
+
"""You are an expert in LangChain.
|
28 |
+
Always answer questions starting with "As a LangChain expert".
|
29 |
+
Question: {question}
|
30 |
+
Answer:"""
|
31 |
+
) | ChatOpenAI(model="gpt-4")
|
32 |
+
)
|
33 |
+
|
34 |
+
openai_chain = (
|
35 |
+
PromptTemplate.from_template(
|
36 |
+
"""You are an expert in OpenAI.
|
37 |
+
Always answer questions starting with "As an OpenAI expert".
|
38 |
+
Question: {question}
|
39 |
+
Answer:"""
|
40 |
+
) | ChatOpenAI(model="gpt-4")
|
41 |
+
)
|
42 |
+
|
43 |
+
general_chain = (
|
44 |
+
PromptTemplate.from_template(
|
45 |
+
"""Respond to the following question:
|
46 |
+
Question: {question}
|
47 |
+
Answer:"""
|
48 |
+
) | ChatOpenAI(model="gpt-4")
|
49 |
+
)
|
50 |
+
|
51 |
+
return classifier_chain, langchain_chain, openai_chain, general_chain
|
52 |
+
|
53 |
+
def route_question(question, openai_key):
|
54 |
+
try:
|
55 |
+
classifier_chain, langchain_chain, openai_chain, general_chain = create_chains(openai_key)
|
56 |
+
|
57 |
+
# Classify the question
|
58 |
+
classification = classifier_chain.invoke({"question": question})
|
59 |
+
|
60 |
+
# Route to appropriate chain
|
61 |
+
if "langchain" in classification.lower():
|
62 |
+
response = langchain_chain.invoke({"question": question})
|
63 |
+
elif "openai" in classification.lower():
|
64 |
+
response = openai_chain.invoke({"question": question})
|
65 |
+
else:
|
66 |
+
response = general_chain.invoke({"question": question})
|
67 |
+
|
68 |
+
return f"Classification: {classification}\nResponse: {response.content}"
|
69 |
+
except Exception as e:
|
70 |
+
return f"Error: {str(e)}"
|
71 |
+
|
72 |
+
# Create Gradio interface
|
73 |
+
demo = gr.Interface(
|
74 |
+
fn=route_question,
|
75 |
+
inputs=[
|
76 |
+
gr.Textbox(label="Enter your question"),
|
77 |
+
gr.Textbox(label="OpenAI API Key", type="password")
|
78 |
+
],
|
79 |
+
outputs=gr.Textbox(label="Response"),
|
80 |
+
title="LangChain Router Demo",
|
81 |
+
description="This demo shows how routing works in LangChain. Ask questions about LangChain, OpenAI, or any other topic."
|
82 |
+
)
|
83 |
+
|
84 |
+
if __name__ == "__main__":
|
85 |
+
demo.launch()
|