import os
from apikey import apikey
import streamlit as st
from langchain.llms import OpenAI
os.environ["OPENAI_API_KEY"] = apikey
st.title('Medium Article Generator')
topic = st.text_input('Input your topic of interest')
连接到 OpenAI LLM
import os
from apikey import apikey
import streamlit as st
from langchain.llms import OpenAI
os.environ["OPENAI_API_KEY"] = apikey
st.title('Medium Article Generator')
topic = st.text_input('Input your topic of interest')
# 新增
llm = OpenAI(temperature=0.9)
if topic:
response = llm(topic)
st.write(response)
提示模板
为了避免重复使用'给我一篇关于…的中等文章',我们将把它作为提示模板处理。
import os
from apikey import apikey
import streamlit as st
from langchain.llms import OpenAI
os.environ["OPENAI_API_KEY"] = apikey
st.title('Medium Article Generator')
topic = st.text_input('Input your topic of interest')
# 从 LangChain 导入提示模板from langchain.prompts import PromptTemplate
# 创建一个 Prompt Template 实例# PromptTemplate 将接受输入变量,在 input_variables 数组中指定了主题和语言作为输入变量
title_template = PromptTemplate(
input_variables=['topic', 'language'],
template='Give me medium article title on {topic} in {language}'
)
llm = OpenAI(temperature=0.9)
if topic:
response = llm(title_template.format(topic=topic, language='english'))
st.write(response)
import os
from apikey import apikey
import streamlit as st
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
os.environ["OPENAI_API_KEY"] = apikey
st.title('Medium Article Generator')
topic = st.text_input('Input your topic of interest')
language = st.text_input('Input language')
title_template = PromptTemplate(
input_variables=['topic', 'language'],
template='Give me medium article title on {topic} in {language}'
)
llm = OpenAI(temperature=0.9)
from langchain.chains import LLMChain
# verbose=True 会详细输出过程
title_chain = LLMChain(llm=llm, prompt=title_template, verbose=True)
if topic:
# 我们的标题模板需要两个输入变量,所以我们使用了一个字典。
response = title_chain.run({'topic': topic, 'language': language})
st.write(response)
输出
> Entering new LLMChain chain...
Prompt after formatting:
Give me medium article title on investing in english
> Finished chain.
单个参数
实现简化一些:输入、参数、调用等方面
import os
from apikey import apikey
import streamlit as st
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
os.environ["OPENAI_API_KEY"] = apikey
st.title('Medium Article Generator')
topic = st.text_input('Input your topic of interest')
title_template = PromptTemplate(
input_variables=['topic'],
template='Give me medium article title on {topic}'
)
llm = OpenAI(temperature=0.9)
from langchain.chains import LLMChain
# verbose=True 会详细输出过程
title_chain = LLMChain(llm=llm, prompt=title_template, verbose=True)
if topic:
# 一个参数只需要一个字符串变量
response = title_chain.run(topic)
st.write(response)
顺序链条
对于更复杂的任务,我们可以利用顺序链。
import os
from apikey import apikey
import streamlit as st
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain
os.environ ["OPENAI_API_KEY"] = apikey
st.title('Medium Article Generator')
topic = st.text_input('Input your topic of interest')
title_template = PromptTemplate(
input_variables = ['topic'],
template = 'Give me medium article title on {topic}'
)
article_template = PromptTemplate(
input_variables = ['title'],
template = 'Give me medium article for {title}'
)
llm = OpenAI(temperature=0.9)
title_chain = LLMChain(llm=llm, prompt=title_template, verbose=True)
article_chain = LLMChain(llm=llm, prompt=article_template, verbose=True)
if topic:
# 首先,根据主题生成标题。
title_response = title_chain.run({'topic': topic})
generated_title = title_response ifisinstance(title_response, str) else title_response.get('content', '')
if generated_title:
# 然后,根据生成的标题生成文章内容。
article_response = article_chain.run({'title': generated_title})
st.write(article_response ifisinstance(article_response, str) else article_response.get('content', ''))
import os
from apikey import apikey
import streamlit as st
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain
os.environ ["OPENAI_API_KEY"] = apikey
st.title('Medium Article Generator')
topic = st.text_input('Input your topic of interest')
title_template = PromptTemplate(
input_variables = ['topic'],
template = 'Give me medium article title on {topic}'
)
article_template = PromptTemplate(
input_variables = ['title'],
template = 'Give me medium article for {title}'
)
llm = OpenAI(temperature=0.9)
title_chain = LLMChain(llm=llm, prompt=title_template, verbose=True)
llm2 = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=0.9)
article_chain = LLMChain(llm=llm2, prompt=article_template, verbose=True)
overall_chain =SimpleSequentialChain(chains=[title_chain, article_chain], verbose=True)
if topic:
response = overall_chain.run(topic)
st.write(response)
运行结果
命令行输出
> Entering new LLMChain chain...Prompt after formatting:Give me medium article for "5 Strategies for Investing Wisely in Today's Markets"> Finished chain.I'm sorry, but I cannot provide you with a specific Medium article as I am an AI language model and do not have direct access to the internet or a database of articles. However, I can offer you a summary of five strategies for investing wisely in today's markets:1. Diversify your portfolio: Diversification involves spreading your investments across different asset classes, sectors, and regions. By diversifying, you reduce the risk of one particular investment negatively impacting your overall portfolio. This strategy allows you to potentially earn profits while minimizing potential losses.2. Conduct thorough research: Knowledge is key when it comes to investing wisely. Before investing, take the time to do thorough research on the companies or assets you plan to invest in. Analyze their financials, industry trends, and competitive advantages. Additionally, stay updated on market news and trends to make informed decisions.3. Set clear investment goals: Define your investment goals and time horizon. Are you investing for short-term gains or long-term wealth accumulation? By setting clear goals, you can tailor your investment strategy accordingly and avoid making impulsive decisions based on short-term market fluctuations.4. Have a long-term perspective: Investing wisely often involves a long-term perspective. Trying to time the market and make short-term gains can be speculative and risky. Instead, focus on investing in solid companies or assets that have long-term growth potential. Warren Buffett famously said, "The stock market is a device for transferring money from the impatient to the patient."5. Control your emotions: Investing in volatile markets can be nerve-wracking, leading to emotional decision making. It's crucial to control your emotions and avoid making impulsive investment choices based on fear or greed. Develop a disciplined approach, stick to your investment plan, and avoid being swayed by short-term market sentiment.Remember, investing always carries some level of risk, and it's advisable to consult with a financial advisor or conduct further research before making any investment decisions.> Finished chain.