...
This commit is contained in:
		
							
								
								
									
										15
									
								
								examples_rust/ai/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								examples_rust/ai/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| [package] | ||||
| name = "openrouter_example" | ||||
| version = "0.1.0" | ||||
| edition = "2021" | ||||
|  | ||||
| [workspace] | ||||
|  | ||||
| [[bin]] | ||||
| name = "openrouter_example" | ||||
| path = "openrouter_example.rs" | ||||
|  | ||||
| [dependencies] | ||||
| codemonkey = { path = "../../packages/ai/codemonkey" } | ||||
| openai-api-rs = "6.0.8" | ||||
| tokio = { version = "1.0", features = ["full"] } | ||||
							
								
								
									
										47
									
								
								examples_rust/ai/openrouter_example.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								examples_rust/ai/openrouter_example.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| use codemonkey::{create_ai_provider, AIProviderType, CompletionRequestBuilder, Message, MessageRole, Content}; | ||||
| use std::error::Error; | ||||
|  | ||||
| #[tokio::main] | ||||
| async fn main() -> Result<(), Box<dyn Error>> { | ||||
|  | ||||
|     let (mut provider, provider_type) = create_ai_provider(AIProviderType::OpenRouter)?; | ||||
|  | ||||
|     let messages = vec![Message { | ||||
|         role: MessageRole::user, | ||||
|         content: Content::Text("Explain the concept of a factory design pattern in Rust.".to_string()), | ||||
|         name: None, | ||||
|         tool_calls: None, | ||||
|         tool_call_id: None, | ||||
|     }]; | ||||
|  | ||||
|     println!("Sending request to OpenRouter..."); | ||||
|     let response = CompletionRequestBuilder::new( | ||||
|         &mut *provider, | ||||
|         "openai/gpt-oss-120b".to_string(), // Model name as specified by the user | ||||
|         messages, | ||||
|         provider_type, // Pass the provider_type | ||||
|     ) | ||||
|     .temperature(1.0) | ||||
|     .max_tokens(8192) | ||||
|     .top_p(1.0) | ||||
|     .reasoning_effort("medium") | ||||
|     .stream(false) | ||||
|     .openrouter_options(|builder| { | ||||
|         builder.provider( | ||||
|             codemonkey::OpenRouterProviderOptionsBuilder::new() | ||||
|                 .order(vec!["cerebras"]) | ||||
|                 .build(), | ||||
|         ) | ||||
|     }) | ||||
|     .completion() | ||||
|     .await?; | ||||
|  | ||||
|     for choice in response.choices { | ||||
|         if let Some(content) = choice.message.content { | ||||
|             print!("{}", content); | ||||
|         } | ||||
|     } | ||||
|     println!(); | ||||
|  | ||||
|     Ok(()) | ||||
| } | ||||
							
								
								
									
										13
									
								
								examples_rust/ai/run.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										13
									
								
								examples_rust/ai/run.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| #!/bin/bash | ||||
| set -e | ||||
|  | ||||
| # Change to directory where this script is located | ||||
| cd "$(dirname "${BASH_SOURCE[0]}")" | ||||
|  | ||||
| source ../../config/myenv.sh | ||||
|  | ||||
| # Build the example | ||||
| cargo build | ||||
|  | ||||
| # Run the example | ||||
| cargo run --bin openrouter_example | ||||
		Reference in New Issue
	
	Block a user