0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

llm

Posted at
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic

# 共通プロンプトテンプレート
prompt = PromptTemplate.from_template("以下の質問に答えてください:{question}")
inputs = {"question": "イーロン・マスクは誰ですか?"}

# モデルA: OpenAI
llm_openai = ChatOpenAI(model="gpt-3.5-turbo")
print("=== OpenAI ===")
print((prompt | llm_openai).invoke(inputs))

# モデルB: Anthropic
llm_anthropic = ChatAnthropic(model="claude-3-opus-20240229")
print("\n=== Anthropic ===")
print((prompt | llm_anthropic).invoke(inputs))

# OpenAIの例(公式 openai パッケージ使用)
import openai
import os

openai.api_key = os.getenv("OPENAI_API_KEY")
response_openai = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "イーロン・マスクは誰ですか?"}],
)
print("=== OpenAI ===")
print(response_openai["choices"][0]["message"]["content"])

# Anthropicの例(公式 anthropic パッケージ使用)
import anthropic

client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
response_anthropic = client.messages.create(
    model="claude-3-opus-20240229",
    max_tokens=1024,
    messages=[{"role": "user", "content": "イーロン・マスクは誰ですか?"}]
)
print("\n=== Anthropic ===")
print(response_anthropic.content[0].text)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?