Update app.py
Browse files
app.py
CHANGED
|
@@ -10,42 +10,32 @@ 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 |
-
|
| 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 =
|
| 44 |
|
| 45 |
# Execute with configuration
|
| 46 |
response = chain.with_config(
|
| 47 |
configurable={
|
| 48 |
-
"style": mode,
|
| 49 |
"temperature": float(temperature)
|
| 50 |
}
|
| 51 |
).invoke({"topic": topic})
|
|
@@ -94,4 +84,4 @@ demo = gr.Interface(
|
|
| 94 |
)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
-
demo.launch()
|
|
|
|
| 10 |
# Set API key
|
| 11 |
os.environ["OPENAI_API_KEY"] = api_key
|
| 12 |
|
| 13 |
+
# Initialize model with configurable temperature
|
| 14 |
model = ChatOpenAI(
|
| 15 |
model="gpt-4o-mini",
|
| 16 |
+
openai_api_key=api_key
|
| 17 |
+
).configurable_fields(
|
| 18 |
+
temperature=ConfigurableField(id="temperature")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
)
|
| 20 |
+
|
| 21 |
+
# Define different prompts based on mode
|
| 22 |
+
if mode == "joke":
|
| 23 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 24 |
+
("system", "You are a helpful assistant skilled in telling jokes."),
|
| 25 |
+
("user", "Tell me a funny joke about {topic}")
|
| 26 |
+
])
|
| 27 |
+
else:
|
| 28 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 29 |
+
("system", "You are a helpful assistant skilled in writing poetry."),
|
| 30 |
+
("user", "Write a beautiful poem about {topic}")
|
| 31 |
+
])
|
| 32 |
|
| 33 |
# Create chain
|
| 34 |
+
chain = prompt | model
|
| 35 |
|
| 36 |
# Execute with configuration
|
| 37 |
response = chain.with_config(
|
| 38 |
configurable={
|
|
|
|
| 39 |
"temperature": float(temperature)
|
| 40 |
}
|
| 41 |
).invoke({"topic": topic})
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
+
demo.launch(share=False)
|