chore: Remove openrouter client

- Remove call to openrouter.play from the main play function
- Used the OpenAI client instead
- Updated the examples
- Updated the README
This commit is contained in:
Mahmoud-Emad
2025-10-29 11:42:44 +03:00
parent 88f83cbfe2
commit f6734a3568
14 changed files with 182 additions and 561 deletions

View File

@@ -1,7 +0,0 @@
!!hero_code.generate_client
name:'openrouter'
classname:'OpenRouter'
singleton:0
default:1
hasconfig:1
reset:0

View File

@@ -1,13 +0,0 @@
module openrouter
fn test_factory() {
mut client := get(name: 'default', create: true)!
assert client.name == 'default'
assert client.url == 'https://openrouter.ai/api/v1'
assert client.model_default == 'qwen/qwen-2.5-coder-32b-instruct'
}
fn test_client_creation() {
mut client := new(name: 'test_client')!
assert client.name == 'test_client'
}

View File

@@ -1,97 +0,0 @@
module openrouter
import json
@[params]
pub struct CompletionArgs {
pub mut:
model string
messages []Message // optional because we can use message, which means we just pass a string
message string
temperature f64 = 0.2
max_completion_tokens int = 32000
}
pub struct Message {
pub mut:
role RoleType
content string
}
pub enum RoleType {
system
user
assistant
function
}
fn roletype_str(x RoleType) string {
return match x {
.system {
'system'
}
.user {
'user'
}
.assistant {
'assistant'
}
.function {
'function'
}
}
}
pub struct ChatCompletion {
pub mut:
id string
created u32
result string
usage Usage
}
// creates a new chat completion given a list of messages
// each message consists of message content and the role of the author
pub fn (mut f OpenRouter) chat_completion(args_ CompletionArgs) !ChatCompletion {
mut args := args_
if args.model == '' {
args.model = f.model_default
}
mut m := ChatMessagesRaw{
model: args.model
temperature: args.temperature
max_completion_tokens: args.max_completion_tokens
}
for msg in args.messages {
mr := MessageRaw{
role: roletype_str(msg.role)
content: msg.content
}
m.messages << mr
}
if args.message != '' {
mr := MessageRaw{
role: 'user'
content: args.message
}
m.messages << mr
}
data := json.encode(m)
mut conn := f.connection()!
r := conn.post_json_str(prefix: 'chat/completions', data: data)!
res := json.decode(ChatCompletionRaw, r)!
mut result := ''
for choice in res.choices {
result += choice.message.content
}
mut chat_completion_result := ChatCompletion{
id: res.id
created: res.created
result: result
usage: res.usage
}
return chat_completion_result
}

View File

@@ -1,140 +0,0 @@
module openrouter
import incubaid.herolib.core.base
import incubaid.herolib.core.playbook { PlayBook }
import incubaid.herolib.ui.console
import json
__global (
openrouter_global map[string]&OpenRouter
openrouter_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string = 'default'
fromdb bool // will load from filesystem
create bool // default will not create if not exist
}
pub fn new(args ArgsGet) !&OpenRouter {
mut obj := OpenRouter{
name: args.name
}
set(obj)!
return get(name: args.name)!
}
pub fn get(args ArgsGet) !&OpenRouter {
mut context := base.context()!
openrouter_default = args.name
if args.fromdb || args.name !in openrouter_global {
mut r := context.redis()!
if r.hexists('context:openrouter', args.name)! {
data := r.hget('context:openrouter', args.name)!
if data.len == 0 {
print_backtrace()
return error('OpenRouter with name: ${args.name} does not exist, prob bug.')
}
mut obj := json.decode(OpenRouter, data)!
set_in_mem(obj)!
} else {
if args.create {
new(args)!
} else {
print_backtrace()
return error("OpenRouter with name '${args.name}' does not exist")
}
}
return get(name: args.name)! // no longer from db nor create
}
return openrouter_global[args.name] or {
print_backtrace()
return error('could not get config for openrouter with name:${args.name}')
}
}
// register the config for the future
pub fn set(o OpenRouter) ! {
mut o2 := set_in_mem(o)!
openrouter_default = o2.name
mut context := base.context()!
mut r := context.redis()!
r.hset('context:openrouter', o2.name, json.encode(o2))!
}
// does the config exists?
pub fn exists(args ArgsGet) !bool {
mut context := base.context()!
mut r := context.redis()!
return r.hexists('context:openrouter', args.name)!
}
pub fn delete(args ArgsGet) ! {
mut context := base.context()!
mut r := context.redis()!
r.hdel('context:openrouter', args.name)!
}
@[params]
pub struct ArgsList {
pub mut:
fromdb bool // will load from filesystem
}
// if fromdb set: load from filesystem, and not from mem, will also reset what is in mem
pub fn list(args ArgsList) ![]&OpenRouter {
mut res := []&OpenRouter{}
mut context := base.context()!
if args.fromdb {
// reset what is in mem
openrouter_global = map[string]&OpenRouter{}
openrouter_default = ''
}
if args.fromdb {
mut r := context.redis()!
mut l := r.hkeys('context:openrouter')!
for name in l {
res << get(name: name, fromdb: true)!
}
return res
} else {
// load from memory
for _, client in openrouter_global {
res << client
}
}
return res
}
// only sets in mem, does not set as config
fn set_in_mem(o OpenRouter) !OpenRouter {
mut o2 := obj_init(o)!
openrouter_global[o2.name] = &o2
openrouter_default = o2.name
return o2
}
pub fn play(mut plbook PlayBook) ! {
if !plbook.exists(filter: 'openrouter.') {
return
}
mut install_actions := plbook.find(filter: 'openrouter.configure')!
if install_actions.len > 0 {
for mut install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
install_action.done = true
}
}
}
// switch instance to be used for openrouter
pub fn switch(name string) {
openrouter_default = name
}

View File

@@ -1,67 +0,0 @@
module openrouter
import incubaid.herolib.data.encoderhero
import incubaid.herolib.core.httpconnection
import os
pub const version = '0.0.0'
const singleton = false
const default = true
@[heap]
pub struct OpenRouter {
pub mut:
name string = 'default'
api_key string
url string = 'https://openrouter.ai/api/v1'
model_default string = 'qwen/qwen-2.5-coder-32b-instruct'
}
// your checking & initialization code if needed
fn obj_init(mycfg_ OpenRouter) !OpenRouter {
mut mycfg := mycfg_
if mycfg.model_default == '' {
k := os.getenv('OPENROUTER_AI_MODEL')
if k != '' {
mycfg.model_default = k
}
}
if mycfg.url == '' {
k := os.getenv('OPENROUTER_URL')
if k != '' {
mycfg.url = k
}
}
if mycfg.api_key == '' {
k := os.getenv('OPENROUTER_API_KEY')
if k != '' {
mycfg.api_key = k
} else {
return error('OPENROUTER_API_KEY environment variable not set')
}
}
return mycfg
}
pub fn (mut client OpenRouter) connection() !&httpconnection.HTTPConnection {
mut c2 := httpconnection.new(
name: 'openrouterconnection_${client.name}'
url: client.url
cache: false
retry: 20
)!
c2.default_header.set(.authorization, 'Bearer ${client.api_key}')
return c2
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj OpenRouter) !string {
return encoderhero.encode[OpenRouter](obj)!
}
pub fn heroscript_loads(heroscript string) !OpenRouter {
mut obj := encoderhero.decode[OpenRouter](heroscript)!
return obj
}

View File

@@ -1,38 +0,0 @@
module openrouter
struct ChatCompletionRaw {
mut:
id string
object string
created u32
choices []ChoiceRaw
usage Usage
}
struct ChoiceRaw {
mut:
index int
message MessageRaw
finish_reason string
}
struct MessageRaw {
mut:
role string
content string
}
struct ChatMessagesRaw {
mut:
model string
messages []MessageRaw
temperature f64 = 0.5
max_completion_tokens int = 32000
}
pub struct Usage {
pub mut:
prompt_tokens int
completion_tokens int
total_tokens int
}

View File

@@ -1,97 +0,0 @@
# OpenRouter V Client
A V client for the OpenRouter API, providing access to multiple AI models through a unified interface.
## Quick Start
```v
import incubaid.herolib.clients.openrouter
import incubaid.herolib.core.playcmds
// Configure client (key can be read from env vars)
playcmds.run(
heroscript: '
!!openrouter.configure name:"default"
key:"${YOUR_OPENROUTER_KEY}"
url:"https://openrouter.ai/api/v1"
model_default:"qwen/qwen-2.5-coder-32b-instruct"
'
reset: false
)!
mut client := openrouter.get()!
// Simple chat example
resp := client.chat_completion(
model: "qwen/qwen-2.5-coder-32b-instruct"
message: "Hello, world!"
temperature: 0.6
)!
println('Answer: ${resp.result}')
```
## Environment Variables
The client automatically reads API keys from environment variables if not explicitly configured:
- `OPENROUTER_API_KEY` - OpenRouter API key
- `AIKEY` - Alternative API key variable
- `AIURL` - API base URL (defaults to `https://openrouter.ai/api/v1`)
- `AIMODEL` - Default model (defaults to `qwen/qwen-2.5-coder-32b-instruct`)
## Example with Multiple Messages
```v
import incubaid.herolib.clients.openrouter
mut client := openrouter.get()!
resp := client.chat_completion(
messages: [
openrouter.Message{
role: .system
content: 'You are a helpful coding assistant.'
},
openrouter.Message{
role: .user
content: 'Write a hello world in V'
},
]
temperature: 0.3
max_completion_tokens: 1024
)!
println(resp.result)
```
## Configuration via Heroscript
```hero
!!openrouter.configure
name: "default"
key: "sk-or-v1-..."
url: "https://openrouter.ai/api/v1"
model_default: "qwen/qwen-2.5-coder-32b-instruct"
```
## Features
- **Chat Completion**: Generate text completions using various AI models
- **Multiple Models**: Access to OpenRouter's extensive model catalog
- **Environment Variable Support**: Automatic configuration from environment
- **Factory Pattern**: Manage multiple client instances
- **Retry Logic**: Built-in retry mechanism for failed requests
## Available Models
OpenRouter provides access to many models including:
- `qwen/qwen-2.5-coder-32b-instruct` - Qwen 2.5 Coder (default)
- `anthropic/claude-3.5-sonnet`
- `openai/gpt-4-turbo`
- `google/gemini-pro`
- `meta-llama/llama-3.1-70b-instruct`
- And many more...
Check the [OpenRouter documentation](https://openrouter.ai/docs) for the full list of available models.

View File

@@ -10,7 +10,6 @@ import incubaid.herolib.clients.meilisearch
import incubaid.herolib.clients.mycelium
import incubaid.herolib.clients.mycelium_rpc
import incubaid.herolib.clients.openai
import incubaid.herolib.clients.openrouter
import incubaid.herolib.clients.postgresql_client
import incubaid.herolib.clients.qdrant
import incubaid.herolib.clients.rcloneclient
@@ -66,7 +65,6 @@ pub fn run_all(args_ PlayArgs) ! {
mycelium.play(mut plbook)!
mycelium_rpc.play(mut plbook)!
openai.play(mut plbook)!
openrouter.play(mut plbook)!
postgresql_client.play(mut plbook)!
qdrant.play(mut plbook)!
rcloneclient.play(mut plbook)!