ניסוי OpenRouter מ Python
05/08/2025
אופןראוטר מספק גישה להמון מודלים מסחריים ומודלי קוד פתוח דרך API אחד, מסך תשלום אחד ומסך אחד בו אפשר לראות את הפעילות והעלויות. זה מאוד נוח כשרוצים לשים לב כמה עולה כל ה AI הזה וגם לחסוך כסף באמצעות גיוון במודלים.
אפשר להירשם כאן: https://openrouter.ai/
וגם לדבר עם המון מודלים מתוך ממשק ה Web שלהם, אבל אני רוצה להריץ ניסוי מתוך פייתון כדי לתת לכמה מודלים לענות לאותה שאילתה. התחלתי עם הקוד הבא:
import asyncio
from agents import Agent, Runner
from agents.extensions.models.litellm_model import LitellmModel
import os
async def main():
models = [
"openrouter/moonshotai/kimi-k2",
"openrouter/z-ai/glm-4.5",
"openrouter/cognitivecomputations/dolphin-mistral-24b-venice-edition:free",
"openrouter/inception/mercury"
]
for model in models:
agent = Agent(
name="Assistant",
model=LitellmModel(model=model),
instructions="You only respond in haikus.",
)
print(f"Model: {model}")
result = await Runner.run(agent, "Tell me about recursion in programming.")
print(result.final_output)
print("---")
if __name__ == "__main__":
asyncio.run(main())
הקוד משתמש בספריית OpenAI Agents SDK אותה אני מאוד מחבב ומבקש מ-4 מודלים להסביר בשיר Haiku מהי רקורסיה. אלה התשובות שקיבלתי:
Model: openrouter/moonshotai/kimi-k2
Function calls itself
Each step unpeels a layer
Base case stops the fall
---
Model: openrouter/z-ai/glm-4.5
Function calls itself
To solve a smaller problem
Base case stops the loop
Code within the code
Stack grows deeper with each call
Until base is found
Divide and conquer
Breaking problems into parts
Solve the simplest
---
Model: openrouter/cognitivecomputations/dolphin-mistral-24b-venice-edition:free
A function calls itself,
Looping back in endless dance,
Chasing its own tail.
Base case breaks the spell,
Else infinite descent,
Stack overflow lovers.
Elegant solutions,
Complex problems simplified,
Beauty in repetition.
But beware the trap,
Unchecked, it can drive you mad,
Like rabbits in a field.
Fibonacci, trees,
Recursion's grace is seen,
In patterns endless woven.
So with care and caution,
Wield this tool, and watch it grow,
A tapestry of code.
---
Model: openrouter/inception/mercury
Function calls itself,
In layers it goes UNCer hands depthHappyLe Finding to end.
סך הכל חוץ ממרקורי כולם היו בסדר. מבחינת עלויות דולפין-מיסטרל הלא מצונזר הוא בחינם אז ברור שהוא הכי משתלם אבל בכל מקרה נצטרך לבדוק את זה על בקשות יותר משמעותיות.
איזה מודלי קוד פתוח אתם הכי אוהבים? ואיך אתם ניגשים אליהם?