first version
This commit is contained in:
27
.gitignore
vendored
27
.gitignore
vendored
@ -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
|
||||
11
build.sbt
Normal file
11
build.sbt
Normal file
@ -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"
|
||||
)
|
||||
45
src/main/scala/chat_sql/main.scala
Normal file
45
src/main/scala/chat_sql/main.scala
Normal file
@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user