OpenRouterTeam / openrouter-examples

Examples of integrating the OpenRouter API
https://openrouter.ai/docs
MIT License
76 stars 10 forks source link

How do I send a `prompt` using the Python OpenAI client? #6

Open NightMachinery opened 5 months ago

NightMachinery commented 5 months ago

The site has this example code for sending ChatML messages:

from openai import OpenAI
from os import getenv

# gets API Key from environment variable OPENAI_API_KEY
client = OpenAI(
  base_url="https://openrouter.ai/api/v1",
  api_key=getenv("OPENROUTER_API_KEY"),
)

completion = client.chat.completions.create(
  model="anthropic/claude-3-opus:beta",
  messages=[
    {
      "role": "user",
      "content": "Say this is a test",
    },
  ],
)
print(completion.choices[0].message.content)

But I can't find how I can send a simple prompt (with no instruct formatting) in Python and get the response streamed.

Related:

pryh4ck commented 1 month ago

I think it was changed to messages now, and to send a traditional prompt, you send it with the "system" role:

from openai import OpenAI client = OpenAI()

completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"} ] )

print(completion.choices[0].message)