chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
748 stars 127 forks source link

APIConnectionError: Connection error. #1155

Closed JRToks closed 1 month ago

JRToks commented 1 month ago

Chaquopy version 15.0.1

Devices or emulators where the issue happens Pixel 6a API 34

Relevant parts of your code

plugins { id("com.android.application") id("org.jetbrains.kotlin.android") id("com.chaquo.python") }

android { namespace = "com.codex.tala" compileSdk = 34

defaultConfig {
    applicationId = "com.codex.tala"
    minSdk = 28
    targetSdk = 34
    versionCode = 1
    versionName = "1.0"

    testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

    ndk {
        // On Apple silicon, you can omit x86_64.
        abiFilters += listOf("arm64-v8a", "x86_64")
    }
}

buildTypes {
    release {
        isMinifyEnabled = false
        proguardFiles(
            getDefaultProguardFile("proguard-android-optimize.txt"),
            "proguard-rules.pro"
        )
    }
}
compileOptions {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

kotlinOptions {
    jvmTarget = "1.8"
}

}

chaquopy { defaultConfig { buildPython("C:/Users/Joshua/AppData/Local/Programs/Python/Python311/python.exe") version = "3.11"

    pip {
        install("pydantic<2")
        install("openai")
    }
}
productFlavors { }
sourceSets {
    getByName("main") {
        srcDir("src/main/python")
    }
}

}

plugins { id("com.android.application") version "8.2.2" apply false id("org.jetbrains.kotlin.android") version "1.9.22" apply false id("com.chaquo.python") version "15.0.1" apply false }

private void addToChat(String message, String sentBy){ runOnUiThread(new Runnable() { @Override public void run() { messageList.add(new Message(message, sentBy)); messageAdapter.notifyDataSetChanged(); recyclerView.smoothScrollToPosition(messageAdapter.getItemCount()); } }); }

private void addResponse(String response){
    messageList.remove(messageList.size()-1);
    addToChat(response, Message.SENT_BY_BOT);
}

private void CallAPI(String question){
    messageList.add(new Message("Typing...", Message.SENT_BY_BOT));
    PyObject module = py.getModule("AIHelper");
    PyObject myFnCallVale = module.get("chat_with_gpt");
    addResponse(myFnCallVale.call(question).toString());
}

from openai import OpenAI client = OpenAI(api_key = "secret")

def chat_with_gpt(prompt): prompt = str(prompt) completion = client.chat.completions.create( model="gpt-3.5-turbo-0125", messages=[ {"role": "system", "content": "You are a helpful assistant that talks very sarcastically."}, {"role": "user", "content": prompt} ] )

return completion.choices[0].message

Describe your issue

I'm creating a chatbot in android studio using openai API in python using chaquopy. when i place hard codes in the python file, it runs as expected and the output in the application is correct but when I use the openai chat completion, it wont let me connect and says connection error.

image

mhsmith commented 1 month ago

You've closed the issue; does this mean you've solved the problem? If so, please post the solution here, as it may be useful to other people.