diff --git a/.gitignore b/.gitignore index 558f79d..a203883 100644 --- a/.gitignore +++ b/.gitignore @@ -1,21 +1,6 @@ -# ---> Scala -*.class -*.log - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -# ---> SBT -# Simple Build Tool -# http://www.scala-sbt.org/release/docs/Getting-Started/Directories.html#configuring-version-control - -dist/* -target/ -lib_managed/ -src_managed/ -project/boot/ -project/plugins/project/ -.history -.cache -.lib/ - +/* +!/src/ +!/LICENSE +!/build.sbt +!/README.md +openai-scala-client.conf \ No newline at end of file diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..8a9464a --- /dev/null +++ b/build.sbt @@ -0,0 +1,11 @@ +name := "chat_sql" + +version := "dev" + +scalaVersion := "3.2.2" +scalaBinaryVersion := "3" + +libraryDependencies ++= Seq( + "io.cequence" %% "openai-scala-client" % "0.3.2", + "org.postgresql" % "postgresql" % "42.6.0" +) diff --git a/src/main/scala/chat_sql/main.scala b/src/main/scala/chat_sql/main.scala new file mode 100644 index 0000000..087465a --- /dev/null +++ b/src/main/scala/chat_sql/main.scala @@ -0,0 +1,45 @@ +package chat_sql + +import io.cequence.openaiscala.service.OpenAIServiceFactory +import akka.actor.ActorSystem +import scala.concurrent.ExecutionContext +import scala.concurrent.Await +import scala.concurrent.duration.Duration +import io.cequence.openaiscala.domain.ModelId +import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings +import io.cequence.openaiscala.domain.MessageSpec +import io.cequence.openaiscala.domain.ChatRole +import java.sql.{Connection, DriverManager, ResultSet} +import scala.io.StdIn.readLine +import org.postgresql.util.PSQLException + +@main def main(args: String*): Unit = { + given ec: ExecutionContext = ExecutionContext.global + given actorSystem: ActorSystem = ActorSystem() + val service = OpenAIServiceFactory() + Class.forName("org.postgresql.Driver") + val con_str = "jdbc:postgresql://localhost:5432/chatSql" + val conn = DriverManager.getConnection(con_str) + val stm = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY) + while (true) { + val input = readLine("Enter a query: ") + val systemInfo = "Convert the following Sentence to an SQL query. Return only SQL" + val completion = Await.result(service.createChatCompletion( + Seq(MessageSpec(ChatRole.System, systemInfo), MessageSpec(ChatRole.User, input)), + settings = CreateChatCompletionSettings( + model = ModelId.gpt_3_5_turbo + )), Duration.Inf) + val query = completion.choices.head.message.content + println(query) + try { + val rs = stm.executeQuery(query) + while(rs.next) { + println(rs.getFloat("price")) + } + } + catch { + case e: PSQLException => println(e) + } + } + conn.close() +}