Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
-
from langchain_core.prompts import
|
| 6 |
from langchain_core.runnables import ConfigurableField
|
| 7 |
|
| 8 |
def process_with_config(topic, temperature, mode, api_key):
|
|
@@ -10,40 +10,43 @@ def process_with_config(topic, temperature, mode, api_key):
|
|
| 10 |
# Set API key
|
| 11 |
os.environ["OPENAI_API_KEY"] = api_key
|
| 12 |
|
| 13 |
-
# Initialize
|
| 14 |
model = ChatOpenAI(
|
| 15 |
model="gpt-4o-mini",
|
| 16 |
openai_api_key=api_key,
|
| 17 |
temperature=0
|
| 18 |
-
).configurable_fields(
|
| 19 |
-
temperature=ConfigurableField(
|
| 20 |
-
id="llm_temperature",
|
| 21 |
-
name="LLM Temperature",
|
| 22 |
-
description="Temperature for GPT-4o-mini response generation"
|
| 23 |
-
)
|
| 24 |
)
|
| 25 |
|
| 26 |
-
# Create
|
| 27 |
-
|
| 28 |
-
"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
alternatives={
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
}
|
| 36 |
)
|
| 37 |
|
| 38 |
# Create chain
|
| 39 |
-
chain =
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
mode_mapping = {"joke": "default", "poem": "poetry"}
|
| 43 |
response = chain.with_config(
|
| 44 |
configurable={
|
| 45 |
-
"
|
| 46 |
-
"
|
| 47 |
}
|
| 48 |
).invoke({"topic": topic})
|
| 49 |
|
|
@@ -73,7 +76,7 @@ demo = gr.Interface(
|
|
| 73 |
),
|
| 74 |
gr.Radio(
|
| 75 |
choices=["joke", "poem"],
|
| 76 |
-
label="
|
| 77 |
value="joke"
|
| 78 |
),
|
| 79 |
gr.Textbox(
|
|
@@ -87,7 +90,7 @@ demo = gr.Interface(
|
|
| 87 |
lines=5
|
| 88 |
),
|
| 89 |
title="🤖 GPT-4o-mini Configuration Demo",
|
| 90 |
-
description="Generate content using GPT-4o-mini with configurable temperature and
|
| 91 |
)
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 6 |
from langchain_core.runnables import ConfigurableField
|
| 7 |
|
| 8 |
def process_with_config(topic, temperature, mode, api_key):
|
|
|
|
| 10 |
# Set API key
|
| 11 |
os.environ["OPENAI_API_KEY"] = api_key
|
| 12 |
|
| 13 |
+
# Initialize model
|
| 14 |
model = ChatOpenAI(
|
| 15 |
model="gpt-4o-mini",
|
| 16 |
openai_api_key=api_key,
|
| 17 |
temperature=0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Create base prompt template
|
| 21 |
+
base_prompt = ChatPromptTemplate.from_messages([
|
| 22 |
+
("system", "You are a helpful assistant."),
|
| 23 |
+
("user", "Tell me a joke about {topic}")
|
| 24 |
+
])
|
| 25 |
+
|
| 26 |
+
# Create poem prompt template
|
| 27 |
+
poem_prompt = ChatPromptTemplate.from_messages([
|
| 28 |
+
("system", "You are a helpful assistant."),
|
| 29 |
+
("user", "Write a poem about {topic}")
|
| 30 |
+
])
|
| 31 |
+
|
| 32 |
+
# Create configurable prompt
|
| 33 |
+
configurable_prompt = base_prompt.configurable_alternatives(
|
| 34 |
+
ConfigurableField(id="style"),
|
| 35 |
+
default_key="joke",
|
| 36 |
alternatives={
|
| 37 |
+
"joke": base_prompt,
|
| 38 |
+
"poem": poem_prompt
|
| 39 |
}
|
| 40 |
)
|
| 41 |
|
| 42 |
# Create chain
|
| 43 |
+
chain = configurable_prompt | model
|
| 44 |
|
| 45 |
+
# Execute with configuration
|
|
|
|
| 46 |
response = chain.with_config(
|
| 47 |
configurable={
|
| 48 |
+
"style": mode,
|
| 49 |
+
"temperature": float(temperature)
|
| 50 |
}
|
| 51 |
).invoke({"topic": topic})
|
| 52 |
|
|
|
|
| 76 |
),
|
| 77 |
gr.Radio(
|
| 78 |
choices=["joke", "poem"],
|
| 79 |
+
label="Style",
|
| 80 |
value="joke"
|
| 81 |
),
|
| 82 |
gr.Textbox(
|
|
|
|
| 90 |
lines=5
|
| 91 |
),
|
| 92 |
title="🤖 GPT-4o-mini Configuration Demo",
|
| 93 |
+
description="Generate content using GPT-4o-mini with configurable temperature and style"
|
| 94 |
)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|