Created
April 16, 2024 02:45
-
-
Save DandyLyons/f21d3ac89e5dcdb4b4bc109d36f5b83d to your computer and use it in GitHub Desktop.
2024-04-15 FoodOrderBot Routes
This file contains hidden or 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
| import Fluent | |
| import OpenAI | |
| import Vapor | |
| // THIS IS THE INTERFACE FOR THE OPENAI API THAT WE'LL USE LATER | |
| //let chatQuery = ChatQuery( | |
| // messages: <#T##[Self.ChatCompletionMessageParam]#>, | |
| // model: .gpt4_turbo_preview, | |
| // frequencyPenalty: <#T##Double?#>, | |
| // logitBias: <#T##[String : Int]?#>, | |
| // logprobs: <#T##Bool?#>, | |
| // maxTokens: <#T##Int?#>, | |
| // n: <#T##Int?#>, | |
| // presencePenalty: <#T##Double?#>, | |
| // responseFormat: <#T##Self.ResponseFormat?#>, | |
| // seed: <#T##Int?#>, | |
| // stop: <#T##Self.Stop?#>, | |
| // temperature: <#T##Double?#>, | |
| // toolChoice: <#T##Self.ChatCompletionFunctionCallOptionParam?#>, | |
| // tools: <#T##[Self.ChatCompletionToolParam]?#>, | |
| // topLogprobs: <#T##Int?#>, | |
| // topP: <#T##Double?#>, | |
| // user: <#T##String?#>, | |
| // stream: <#T##Bool#> | |
| //) | |
| func routes(_ app: Application) throws { | |
| app.get { req async in | |
| "Yes, the server is running." | |
| } | |
| // POST request to /chat/ | |
| app.post("chat") { req async throws in | |
| guard let chatRequest = try? req.content.decode(ChatRequest.self) else { | |
| let errorResponse = ErrorResponse(error: "Invalid chat request") | |
| return try await errorResponse.encodeResponse(status: .badRequest, for: req) | |
| } | |
| // TODO: Process chat request | |
| let messageResponse = "message" | |
| let foodItems: [FoodItem] = [] | |
| let body = ChatAndFoodItems(message: messageResponse, foodItems: foodItems) | |
| return try await body.encodeResponse(status: .ok, for: req) | |
| } | |
| app.post("confirm-order") { req async throws in | |
| guard let confirmRequest = try? req.content.decode(ConfirmRequest.self) else { | |
| let error = ErrorResponse(error: "Invalid confirm request") | |
| return try await error.encodeResponse(status: .badRequest, for: req) | |
| } | |
| do { | |
| // TODO: Process | |
| let bill = Bill( | |
| total: 9.99, | |
| items: [FoodItem( | |
| name: "burger", | |
| description: "with cheese", | |
| quantity: 1, | |
| price: 9.99 | |
| )], | |
| chatMessages: [ChatMessage(message: "Thank you for ordering")] | |
| ) | |
| return try await bill.encodeResponse(status: .ok, for: req) | |
| } catch { | |
| return try await ErrorResponse(error: "Something went wrong") | |
| .encodeResponse(status: .internalServerError, for: req) | |
| } | |
| } | |
| app.post("pay") { req async throws in | |
| guard let confirmRequest = try? req.content.decode(PayRequest.self) else { | |
| let error = ErrorResponse(error: "Invalid pay request") | |
| return try await error.encodeResponse(status: .badRequest, for: req) | |
| } | |
| do { | |
| let responseObj = Bill(total: 0.99, items: [], chatMessages: [.init(message: "This is an example message")]) | |
| return try await responseObj.encodeResponse(status: .ok, for: req) | |
| } catch { | |
| return try await ErrorResponse(error: "Something went wrong") | |
| .encodeResponse(status: .internalServerError, for: req) | |
| } | |
| } | |
| app.get("menu") { req async throws in | |
| let menu: [FoodItemTable] = try await FoodItemTable.query(on: req.db).all() | |
| return try await menu.encodeResponse(for: req) | |
| } | |
| app.get("menu", ":category") { req async throws in | |
| guard let category = req.parameters.get("category") else { | |
| throw Abort(.internalServerError) | |
| } | |
| let menu = try await CategoryTable.query(on: req.db) | |
| .filter(\CategoryTable.$category == category) | |
| .all() | |
| return try await menu.encodeResponse(for: req) | |
| } | |
| } | |
| // MARK: HTTP Request (Client -> Server) | |
| import Foundation | |
| import Vapor | |
| // Content is a Vapor type that adds http functionality to Codable | |
| struct ChatRequest: Content { | |
| let promptString: String | |
| let foodItems: [FoodItem] | |
| } | |
| struct ConfirmRequest: Content { | |
| let foodItems: [FoodItem] | |
| } | |
| struct PayRequest: Content { | |
| let total: Double | |
| let items: [FoodItem] | |
| let paymentMethod: String | |
| } | |
| // MARK: HTTP Responses (Server -> Client) | |
| struct ChatAndFoodItems: Content, Codable { | |
| let message: String | |
| let foodItems: [FoodItem] | |
| } | |
| struct FoodItem: Content, Codable { | |
| let name: String | |
| let description: String | |
| let quantity: Int64 | |
| let price: Double | |
| } | |
| struct Bill: Content { | |
| let total: Double | |
| let items: [FoodItem] | |
| let chatMessages: [ChatMessage] | |
| } | |
| struct ChatMessage: Content { | |
| let message: String | |
| } | |
| struct ErrorResponse: Content, ResponseEncodable, AsyncResponseEncodable { | |
| let error: String | |
| } | |
| struct ErrorResponse: Content, ResponseEncodable, AsyncResponseEncodable { | |
| let error: String | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment