How to integrate Open AI Chat GPT model "gpt-3.5-turbo" in your Android app?

Sdílet
Vložit
  • čas přidán 8. 09. 2024
  • In this video it shows the steps to integrate the Chat GPT APIs of "gpt-3.5-turbo" model in your Android App. It uses Android's volley library to call the GPT APIs from the Json object.
    I hope you like this video. For any questions, suggestions or appreciation please contact us at: programmerworl... or email at: programmerworld1990@gmail.com
    Complete source code and other details/ steps of this video are posted in the below link:
    programmerworl...
    However, the main Java code is copied below also for reference:
    package com.programmerworld.chatgpt35integration;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import androidx.appcompat.app.AppCompatActivity;
    import com.android.volley.AuthFailureError;
    import com.android.volley.DefaultRetryPolicy;
    import com.android.volley.NetworkResponse;
    import com.android.volley.Request;
    import com.android.volley.Response;
    import com.android.volley.RetryPolicy;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.JsonObjectRequest;
    import com.android.volley.toolbox.Volley;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import java.util.HashMap;
    import java.util.Map;
    public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private String stringURLEndPoint = "api.openai.com...";
    private String stringAPIKey = "sk-8pKdd8YoyxxxxxxxxxxXXXXXbkFJX2tPPkoXzpegNelf8NS5";
    private String stringOutput = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);
    }
    public void buttonChatGPT(View view){
    JSONObject jsonObject = new JSONObject();
    try {
    jsonObject.put("model", "gpt-3.5-turbo");
    JSONArray jsonArrayMessage = new JSONArray();
    JSONObject jsonObjectMessage = new JSONObject();
    jsonObjectMessage.put("role", "user");
    jsonObjectMessage.put("content", "Write a poem about clouds");
    jsonArrayMessage.put(jsonObjectMessage);
    jsonObject.put("messages", jsonArrayMessage);
    } catch (JSONException e) {
    throw new RuntimeException(e);
    }
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
    stringURLEndPoint, jsonObject, new Response.Listener ANGULAR_BRACKET JSONObject ANGULAR_BRACKET () {
    @Override
    public void onResponse(JSONObject response) {
    String stringText = null;
    try {
    stringText = response.getJSONArray("choices")
    .getJSONObject(0)
    .getJSONObject("message")
    .getString("content");
    } catch (JSONException e) {
    throw new RuntimeException(e);
    }
    stringOutput = stringOutput + stringText;
    textView.setText(stringOutput);
    }
    }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
    }
    }){
    @Override
    public MapANGULAR_BRACKETString, StringANGULAR_BRACKET getHeaders() throws AuthFailureError {
    MapANGULAR_BRACKETString, StringANGULAR_BRACKET mapHeader = new HashMapANGULAR_BRACKET();
    mapHeader.put("Authorization", "Bearer " + stringAPIKey);
    mapHeader.put("Content-Type", "application/json");
    return mapHeader;
    }
    @Override
    protected Response ANGULAR_BRACKET JSONObjectANGULAR_BRACKET parseNetworkResponse(NetworkResponse response) {
    return super.parseNetworkResponse(response);
    }
    };
    int intTimeoutPeriod = 60000; // 60 seconds timeout duration defined
    RetryPolicy retryPolicy = new DefaultRetryPolicy(intTimeoutPeriod,
    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    jsonObjectRequest.setRetryPolicy(retryPolicy);
    Volley.newRequestQueue(getApplicationContext()).add(jsonObjectRequest);
    }
    }
    --

Komentáře • 11

  • @prasoonpandey8999
    @prasoonpandey8999 Před 4 měsíci +1

    Thanks my friend. gpt-3.5-turbo model worked for me, appreciate your efforts, great video.

  • @A2K37THPTTANHLINH
    @A2K37THPTTANHLINH Před 7 měsíci +3

    SEND FOR ALL USER : model in this code was deleted, you can replace this by "gpt-3.5-turbo-instruct"

  • @paragbharadia2895
    @paragbharadia2895 Před 6 měsíci

    how do you learn this, i also want to learn to figure out on my own how to write code by looking into gpt document??

    • @ProgrammerWorld
      @ProgrammerWorld  Před 5 měsíci +1

      Check the API documents. They are good starting point to learn the code of these tools.

  • @didiwaytrooo
    @didiwaytrooo Před 7 měsíci

    is this api with prices ? and how much bro ?

    • @ProgrammerWorld
      @ProgrammerWorld  Před 7 měsíci

      Yes, these APIs are not free. We need to pay as per the usage. Exact charges at any point of time can be referred on the Open AI website.
      Details of this video is also available at:
      programmerworld.co/android/how-to-integrate-open-ai-chat-gpt-model-gpt-3-5-turbo-in-your-android-app/
      --

  • @uchihatashi3249
    @uchihatashi3249 Před 9 měsíci

    How to make API cheaper?

    • @didiwaytrooo
      @didiwaytrooo Před 7 měsíci

      is this api with prices ? and how much bro ?

  • @A2K37THPTTANHLINH
    @A2K37THPTTANHLINH Před 7 měsíci

    why i have a bug android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules" ? :v

    • @ProgrammerWorld
      @ProgrammerWorld  Před 7 měsíci

      These are default parameters in the manifest file of the app:
      android:dataExtractionRules="@xml/data_extraction_rules"
      android:fullBackupContent="@xml/backup_rules"
      If it is giving error then most likely your development environment is not set correctly. "data_extraction_rules" and "backup_rules" files are missing from the res/xml folder.
      One of the easiest workaround to fix this would be to mark them as false.
      android:dataExtractionRules="false"
      android:fullBackupContent="false"
      Hope it works.
      Details of this video is also available at:
      programmerworld.co/android/how-to-integrate-open-ai-chat-gpt-model-gpt-3-5-turbo-in-your-android-app/
      -

    • @A2K37THPTTANHLINH
      @A2K37THPTTANHLINH Před 7 měsíci

      @@ProgrammerWorld hi but now i have a new bug, when chatbot reply it said: failed due to + .... i think it error because respone.issucess alway = false due to cant working and when i print respone it is error code 404