Skip to main content

llm_test_bench_core/api/
graphql.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7//! GraphQL API implementation.
8
9use async_graphql::{
10    Context, EmptySubscription, Object, Schema, SimpleObject, InputObject,
11};
12use chrono::{DateTime, Utc};
13
14/// GraphQL schema type
15pub type GraphQLSchema = Schema<Query, Mutation, EmptySubscription>;
16
17/// GraphQL completion input
18#[derive(InputObject)]
19pub struct CompletionInput {
20    pub provider: String,
21    pub model: String,
22    pub prompt: String,
23    pub max_tokens: Option<i32>,
24    pub temperature: Option<f32>,
25}
26
27/// GraphQL completion result
28#[derive(SimpleObject)]
29pub struct CompletionResult {
30    pub text: String,
31    pub model: String,
32    pub request_id: String,
33    pub created_at: DateTime<Utc>,
34}
35
36/// GraphQL evaluation input
37#[derive(InputObject)]
38pub struct EvaluationInput {
39    pub provider: String,
40    pub model: String,
41    pub input: String,
42    pub output: String,
43    pub expected: Option<String>,
44    pub metrics: Vec<String>,
45}
46
47/// GraphQL evaluation result
48#[derive(SimpleObject)]
49pub struct EvaluationResult {
50    pub score: f64,
51    pub evaluation_id: String,
52    pub created_at: DateTime<Utc>,
53}
54
55/// GraphQL plugin info
56#[derive(SimpleObject)]
57pub struct PluginNode {
58    pub id: String,
59    pub name: String,
60    pub version: String,
61    pub plugin_type: String,
62    pub status: String,
63}
64
65/// GraphQL Query root
66pub struct Query;
67
68#[Object]
69impl Query {
70    /// Get API version
71    async fn version(&self) -> &str {
72        crate::api::API_VERSION
73    }
74
75    /// Health check
76    async fn health(&self) -> bool {
77        true
78    }
79
80    /// List plugins
81    async fn plugins(&self, _ctx: &Context<'_>) -> Vec<PluginNode> {
82        // Would fetch from plugin system
83        vec![]
84    }
85
86    /// Get plugin by ID
87    async fn plugin(&self, _ctx: &Context<'_>, id: String) -> Option<PluginNode> {
88        // Would fetch from plugin system
89        Some(PluginNode {
90            id,
91            name: "example".to_string(),
92            version: "1.0.0".to_string(),
93            plugin_type: "evaluator".to_string(),
94            status: "ready".to_string(),
95        })
96    }
97}
98
99/// GraphQL Mutation root
100pub struct Mutation;
101
102#[Object]
103impl Mutation {
104    /// Create completion
105    async fn create_completion(
106        &self,
107        _ctx: &Context<'_>,
108        input: CompletionInput,
109    ) -> CompletionResult {
110        // Would call provider
111        CompletionResult {
112            text: "Generated response".to_string(),
113            model: input.model,
114            request_id: uuid::Uuid::new_v4().to_string(),
115            created_at: Utc::now(),
116        }
117    }
118
119    /// Create evaluation
120    async fn create_evaluation(
121        &self,
122        _ctx: &Context<'_>,
123        _input: EvaluationInput,
124    ) -> EvaluationResult {
125        // Would call evaluator
126        EvaluationResult {
127            score: 0.85,
128            evaluation_id: uuid::Uuid::new_v4().to_string(),
129            created_at: Utc::now(),
130        }
131    }
132}
133
134/// GraphQL API
135pub struct GraphQLApi;
136
137impl GraphQLApi {
138    /// Create GraphQL schema
139    pub fn schema() -> GraphQLSchema {
140        Schema::build(Query, Mutation, EmptySubscription).finish()
141    }
142}