Skip to content

Instantly share code, notes, and snippets.

@PonDad
Created September 4, 2023 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PonDad/fc08e8d3de6e096f2087823966260f54 to your computer and use it in GitHub Desktop.
Save PonDad/fc08e8d3de6e096f2087823966260f54 to your computer and use it in GitHub Desktop.
OpenAI Functions Guide

OpenAI functions

OpenAI Functionsは、特定のOpenAIモデル(gpt-3.5-turbo-0613やgpt-4-0613など)が、関数の呼び出しを検出し、それに対して渡すべき引数を含むJSONオブジェクトを出力するように微調整されたものです。API呼び出しで関数を記述し、モデルがそれらの関数を呼び出すための引数を出力するように選択できます。OpenAI Function APIの目標は、汎用のテキスト補完やチャットAPIよりも、より確実で有用な関数呼び出しを返すことです。

OpenAI Functions Agentは、これらのモデルと連携するように設計されています。プログラムでは、必要なLangChainパッケージをインストールし、LLM(言語モデル)やツールを初期化してOpenAI Functions Agentをセットアップしています。Agentは、質問に対する回答や数学に関する質問に対する計算など、さまざまなタスクに対応できます。プログラム内の具体的な処理例も示されています。

pip install openai google-search-results
from langchain import LLMMathChain, OpenAI, SerpAPIWrapper, SQLDatabase, SQLDatabaseChain
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
search = SerpAPIWrapper()
llm_math_chain = LLMMathChain.from_llm(llm=llm, verbose=True)
db = SQLDatabase.from_uri("sqlite:///../../../../../notebooks/Chinook.db")
db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
tools = [
    Tool(
        name = "Search",
        func=search.run,
        description="useful for when you need to answer questions about current events. You should ask targeted questions"
    ),
    Tool(
        name="Calculator",
        func=llm_math_chain.run,
        description="useful for when you need to answer questions about math"
    ),
    Tool(
        name="FooBar-DB",
        func=db_chain.run,
        description="useful for when you need to answer questions about FooBar. Input should be in the form of a question containing full context"
    )
]

agent = initialize_agent(tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True)

agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")

OpenAI Multi Functions Agent

OpenAI Multi Functions Agentは、大規模言語モデルを使用してユーザーのプロンプトに応答するエージェントの使用例を示しています。具体的なツールやAPIキーの設定など、プログラム内での詳細な設定も説明されています。エージェントは、検索機能を使用する能力を持っており、SerpAPIWrapperを介して検索を実行します。エージェントの初期化と設定に関する情報も提供されています。

プログラムは、指定されたプロンプトに基づいて検索を実行し、その結果を出力します。例として、"What is the weather in NYC today, yesterday, and the day before?"という質問に対して、最終的には前日の天気情報が取得できなかったことが示されています。設定にはmax_iterationsの設定も含まれており、長時間のループを回避するために使用されています。

pip install openai google-search-results
from langchain import SerpAPIWrapper
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI

import getpass
import os

os.environ["SERPAPI_API_KEY"] = getpass.getpass()

# Initialize the OpenAI language model
# Replace <your_api_key> in openai_api_key="<your_api_key>" with your actual OpenAI key.
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")

# Initialize the SerpAPIWrapper for search functionality
# Replace <your_api_key> in serpapi_api_key="<your_api_key>" with your actual SerpAPI key.
search = SerpAPIWrapper()

# Define a list of tools offered by the agent
tools = [
    Tool(
        name="Search",
        func=search.run,
        description="Useful when you need to answer questions about current events. You should ask targeted questions.",
    ),
]

mrkl = initialize_agent(
    tools, llm, agent=AgentType.OPENAI_MULTI_FUNCTIONS, verbose=True
)

# Do this so we can see exactly what's going on under the hood
import langchain

langchain.debug = True

mrkl.run("What is the weather in LA and SF?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment