py -m pip install langchain py -m pip install openai
import os os.environ["OPENAI_API_KEY"] = "API KEY" from langchain.llms import OpenAI # LLMラッパーを初期化 llm = OpenAI(temperature=0.9) # 会社名を生成する質問を作成 text = "カラフルなソックスを製造する会社にとって、どのような会社名が良いでしょうか?" # LLMに質問を投げて回答を取得 print(llm(text))
出力結果 Rainbow Sockery、Sock Spot、Colorful Toez、Happy Feet、Socksationなど
pip install streamlit openai langchain
import streamlit as st from langchain.llms import OpenAI from langchain import PromptTemplate st.set_page_config(page_title="🦜🔗 Blog Outline Generator App") st.title('🦜🔗 Blog Outline Generator App') openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password') def generate_response(topic): llm = OpenAI(model_name='text-davinci-003', openai_api_key=openai_api_key) # Prompt template = 'As an experienced data scientist and technical writer, generate an outline for a blog about {topic}.' prompt = PromptTemplate(input_variables=['topic'], template=template) prompt_query = prompt.format(topic=topic) # Run LLM model and print out response response = llm(prompt_query) return st.info(response) with st.form('myform'): topic_text = st.text_input('Enter keyword:', '') submitted = st.form_submit_button('Submit') if not openai_api_key.startswith('sk-'): st.warning('Please enter your OpenAI API key!', icon='⚠') if submitted and openai_api_key.startswith('sk-'): generate_response(topic_text)