Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name = "prisma-gpt"
version = "0.1.0"
edition = "2021"

[bin]
[[bin]]
name = "prisma-gpt"
prisma-gpt = "./src/main.rs"

[dependencies]
Expand Down
24 changes: 17 additions & 7 deletions src/gpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,33 @@ use serde::Deserialize;
use serde_json::json;
use std::fmt;

const MODEL_NAME: &str = "gpt-3.5-turbo-16k"; // Define the model name here

#[derive(Deserialize, Debug)]
pub struct GPTResponse {
id: String,
object: String,
created: u64,
model: String, // Model is a string
choices: Vec<Choice>,
}

#[derive(Deserialize, Debug)]
pub struct Choice {
text: String,
message: Message,
}

#[derive(Deserialize, Debug)]
pub struct Message {
role: String,
content: String,
}

impl fmt::Display for GPTResponse {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut choices_str = String::new();
for choice in &self.choices {
choices_str.push_str(&format!("{}\n", choice.text));
choices_str.push_str(&format!("Role: {}, Content: {}\n", choice.message.role, choice.message.content));
}
write!(f, "{}", choices_str)
}
Expand Down Expand Up @@ -45,15 +57,13 @@ impl GPTService {
text: &str,
) -> Result<GPTResponse, Box<dyn std::error::Error>> {
let client = &self.client;

let openai_api_key = &self.openai_api_key;

let url = "https://api.openai.com/v1/completions";
let url = "https://api.openai.com/v1/chat/completions";
let body = json!({
"prompt": text,
"temperature": 0.7,
"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": text}],
"max_tokens": 2000,
"model": "text-davinci-003",
"model": MODEL_NAME, // use the model name constant here
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
Expand Down
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ async fn main() {
.get_cleaned_schema();

let prompt = format!("{}
As a senior analyst, given the above schemas and data, write a detailed and correct Postgres sql query to answer the analytical question:
{}
Comment the query with your logic.
As a senior analyst, given the above schemas and data, write a detailed and correct mySQL sql query to answer the analytical question: {}
", schema, &question);

print!("{}", &question);
print!("{}", &prompt);

let openai_key = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set");

Expand Down