Код: Выделить всё
fun makeAPIRequest(message: String) {
// Launch a coroutine to handle the API request asynchronously
GlobalScope.launch(Dispatchers.IO) {
// Create a ChatCompletionRequest to send to OpenAI
val chatRequest = ChatCompletionRequest(
model = ModelId("gpt-3.5-turbo-1106"), // Use the correct model name
messages = mutableListOf(
ChatMessage(
role = Role.User, // User sends the message
content = message // The message entered by the user
)
),
temperature = 0.7,
maxTokens = 150
)
// Send the request to OpenAI API and get the response asynchronously
val response = openAI.chatCompletion(chatRequest)
// Process the response on the main thread
withContext(Dispatchers.Main) {
val responseMessage = response.choices.first().message
// Handle the response here, e.g., update UI or process data
chatGPTResponse = responseMessage.toString()
}
}
}
Нет синтаксических ошибок, когда проект просто находится внутри IDE. Однако, когда я пытаюсь запустить приложение, оно мгновенно выходит из строя и выдает мне этот вывод в Logcat:
`
java.lang.NoClassDefFoundError: Не удалось разрешить: Lio/ktor/client/plugins/contentnegotiation/ ContentNegotiation;
...
Вызвано: java.lang.ClassNotFoundException: не найдено класс «io.ktor.client.plugins.contentnegotiation.ContentNegotiation» по пути: DexPathList[[zip-файл "/data/app/~~PDlu8hz4gyE6dIA4WjsxSA==/com.example.worldcrafter-jSXV8Rrpgh6Ga_DxfOZ7qg==/base.apk"],nativeLibrar yDirectories=[/data/app/~~PDlu8hz4gyE6dIA4WjsxSA==/com.example.worldcrafter-jSXV8Rrpgh6Ga_DxfOZ7qg==/lib/x86_64, /data/app/~~PDlu8hz4gyE6dIA4WjsxSA==/com.example.worldcrafter-jSXV8Rrpgh6Ga_DxfOZ7qg==/base.apk!/lib/x86_64, /system/lib64, /system_ext/lib64]]
Код: Выделить всё
The reason I point out that second and longer replica of the first error is because before the app even starts, there is 2 warnings that It cannot access that exact file pathcom.example.worldcrafter W Невозможно открыть '/data/app/~~PDlu8hz4gyE6dIA4WjsxSA==/com.example.worldcrafter-jSXV8Rrpgh6Ga_DxfOZ7qg==/base.dm': Нет такой файл или каталог
`
Вот мои реализации:
Код: Выделить всё
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.recyclerview)
implementation(libs.material)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
implementation("com.aallam.openai:openai-client:3.8.2")
implementation(platform(libs.ktor.bom))
//not using the toml file because i hate it with a burning passion
implementation("io.ktor:ktor-client-core:3.0.2")
implementation("io.ktor:ktor-client-android:3.0.2")
implementation("io.ktor:ktor-client-content-negotiation:3.0.2")
implementation("io.ktor:ktor-serialization-kotlinx-json:3.0.2")
implementation("io.ktor:ktor-client-logging:3.0.2")
implementation("io.ktor:ktor-client-cio:3.0.2")
}
Код: Выделить всё
package com.example.worldcrafter
import android.os.Bundle
import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.aallam.openai.api.chat.*
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.api.core.Role
import com.aallam.openai.api.http.Timeout
import com.aallam.openai.client.OpenAI
import com.aallam.openai.client.OpenAIConfig
import com.example.worldcrafter.ui.adapters.ChatAdapter
import com.google.android.material.textfield.TextInputEditText
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
// App startup
class MainActivity() : ComponentActivity() {
val openAI = OpenAI(OpenAIConfig(BuildConfig.OPENAI_API_KEY, timeout = Timeout(socket = 60.seconds)))
var chatGPTResponse: String = ""
// on creation of app
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Initialize the buttons in the Chat layout
private fun initChatButtons() {
// Setup navigation buttons
setupButton(R.id.btSavedChats2) { switchLayout(LayoutType.SAVED_CHATS) }
setupButton(R.id.btSettings2) { switchLayout(LayoutType.SETTINGS) }
// Initialize mutable list to store messages
val messages = mutableListOf()
// Set up RecyclerView and Adapter
val recyclerView = findViewById(R.id.recyclerView)
val chatAdapter = ChatAdapter(messages) // Use ChatAdapter directly
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = chatAdapter
// Reference to TextInputEditText
val inputEditText = findViewById(R.id.textInputEditText)
// Set an action listener to detect "Enter" key
inputEditText.setOnEditorActionListener { _, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE || (event != null && event.keyCode == KeyEvent.KEYCODE_ENTER)) {
val message = inputEditText.text.toString().trim()
if (message.isNotEmpty()) {
// Use addMessage() instead of direct list manipulation
chatAdapter.addMessage(message)
// Scroll to the latest message
recyclerView.smoothScrollToPosition(messages.size - 1)
//chatgpt message here
makeAPIRequest(message)
// Add the response to the list
chatAdapter.addMessage(chatGPTResponse)
recyclerView.smoothScrollToPosition(messages.size - 1)
// Clear the input field
inputEditText.text?.clear()
// Hide the keyboard after the message is added
hideKeyboard()
return@setOnEditorActionListener true
}
}
false
}
}
//chatgpt stuff
@OptIn(DelicateCoroutinesApi::class)
fun makeAPIRequest(message: String) {
// Launch a coroutine to handle the API request asynchronously
GlobalScope.launch(Dispatchers.IO) {
// Create a ChatCompletionRequest to send to OpenAI
val chatRequest = ChatCompletionRequest(
model = ModelId("gpt-3.5-turbo-1106"), // Use the correct model name
messages = mutableListOf(
ChatMessage(
role = Role.User, // User sends the message
content = message // The message entered by the user
)
),
temperature = 0.7,
maxTokens = 150
)
// Send the request to OpenAI API and get the response asynchronously
val response = openAI.chatCompletion(chatRequest)
// Process the response on the main thread
withContext(Dispatchers.Main) {
val responseMessage = response.choices.first().message
// Handle the response here, e.g., update UI or process data
chatGPTResponse = responseMessage.toString()
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... -done-with
Мобильная версия