-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add code_interpreter tools
- Loading branch information
Showing
6 changed files
with
62 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ serde_json = "1.0" | |
|
||
[dev-dependencies] | ||
tokio-test = "0.4" | ||
tower= "0.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,73 @@ | ||
use axum::{routing::post, Router}; | ||
use rust_bot::assistant::{assistant_chat_handler, create_assistant, DB}; | ||
use sqlx::{Executor, SqlitePool}; | ||
use std::net::SocketAddr; | ||
use tokio::sync::oneshot; | ||
use rust_bot::assistant::{assistant_chat_handler, create_assistant, teardown_assistant, create_files, DB}; // Replace `your_crate` with the actual crate name | ||
use axum::{ | ||
extract::Extension, | ||
body::Body, | ||
http::{Request, StatusCode}, | ||
response::Response, | ||
routing::post, | ||
Router, | ||
}; | ||
|
||
use tower::ServiceExt; // Import the `ServiceExt` trait | ||
// Define a function to create the Axum app with the database pool and assistant. | ||
async fn app(db_pool: SqlitePool, assistant_id: String) -> Router { | ||
// ... (same as in your main file) | ||
Router::new() | ||
.route("/assistant", post(assistant_chat_handler)) // Updated route | ||
.layer(Extension(db_pool)) | ||
.layer(Extension(assistant_id)) // Add the assistant ID as a layer | ||
} | ||
#[tokio::test] | ||
async fn test_chat_endpoint() { | ||
// Set up a temporary in-memory SQLite database for testing | ||
let db_pool = SqlitePool::connect(":memory:").await.unwrap(); | ||
// Run database migrations here if necessary | ||
// sqlx::migrate!("./migrations").run(&db_pool).await.unwrap(); | ||
let files = match create_files( | ||
"context", | ||
Vec::new(), | ||
) | ||
.await | ||
{ | ||
Ok(files) => files, | ||
Err(e) => { | ||
eprintln!("Failed to create files: {:?}", e); | ||
return; | ||
} | ||
}; | ||
// Create an assistant for testing | ||
let assistant = create_assistant( | ||
"Test Assistant", | ||
"gpt-4", | ||
"Your instructions here", | ||
"/context", | ||
files, | ||
) | ||
.await | ||
.unwrap(); | ||
// Start the server using the `app` function | ||
// Create the router | ||
let router = app(db_pool.clone(), assistant.id).await; | ||
let (tx, rx) = oneshot::channel::<()>(); | ||
let server = axum::Server::bind(&"127.0.0.1:0".parse::<SocketAddr>().unwrap()) | ||
.serve(router.into_make_service()) | ||
.with_graceful_shutdown(async { | ||
rx.await.ok(); | ||
}); | ||
let (server, addr) = tokio::spawn(server).await.unwrap(); | ||
// Perform the test | ||
let client = reqwest::Client::new(); | ||
let user_id = "test_user"; | ||
let response = client | ||
.post(format!("http://{}/chat", addr)) | ||
.json(&serde_json::json!({ | ||
"user_id": user_id, | ||
"message": "Hello, chatbot!" | ||
})) | ||
.send() | ||
.await | ||
// Create a mock request | ||
let request = Request::builder() | ||
.uri("/assistant") | ||
.method("POST") | ||
.header("content-type", "application/json") | ||
.body(Body::from( | ||
serde_json::json!({ | ||
"user_id": "user_123", | ||
"message": "Can you help me?" | ||
}) | ||
.to_string(), | ||
)) | ||
.unwrap(); | ||
assert_eq!(response.status(), reqwest::StatusCode::OK); | ||
let response_json: serde_json::Value = response.json().await.unwrap(); | ||
// ... (rest of your test assertions) | ||
// Shut down the server | ||
tx.send(()).unwrap(); | ||
server.await.unwrap(); | ||
// Call the service directly without a server | ||
let response = router.oneshot(request).await.unwrap(); | ||
// Check the response | ||
assert_eq!(response.status(), StatusCode::OK); | ||
// You can also deserialize the response body if needed | ||
// ... | ||
// Clean up files and assistant | ||
teardown_assistant(assistant).await.unwrap(); | ||
files.delete().await.unwrap(); | ||
} |