Browse Source

Refactor and address comments

- Remove unused settings
- Refactor some methods
- Addressed comments
- Improve readme
Riandy 5 months ago
parent
commit
b98f533e87

+ 6 - 3
getting-started/demo/ArticleSummarizer/README.md

@@ -13,10 +13,13 @@ This is a sample Android app to demonstrate Llama 4 multimodal and multilingual
 4. Inside the app, tap on settings icon on top right
 4. Inside the app, tap on settings icon on top right
 5. Configure the Remote URL endpoint (any supported providers that serve Llama 4 models. For example: https://api.together.xyz)
 5. Configure the Remote URL endpoint (any supported providers that serve Llama 4 models. For example: https://api.together.xyz)
 6. Select the desired model from the drop down list. If you need to add more models, modify `ModelUtils.java`
 6. Select the desired model from the drop down list. If you need to add more models, modify `ModelUtils.java`
+7. Go back to the Main chat window
+8. Press the '+' button on the bottom left and select an image article (or take a picture of one!)
+9. Select the 'globe' button on the bottom left and select your languages
+10. Enter a prompt like "summarize this" and press Enter!
+
+> **_NOTE:_**  This is an example project to demonstrate E2E flow. You should NOT use/store API key directly on client. Exposing your API key in client-side environments allows malicious users to take that key and make requests on your behalf. Requests should always be routed through your own backend server where you can keep your API key secure.
 
 
-```
-Note: This is an example project to demonstrate E2E flow. You should NOT use/store API key directly on client. Exposing your API key in client-side environments allows malicious users to take that key and make requests on your behalf. Requests should always be routed through your own backend server where you can keep your API key secure.
-```
 
 
 ## Reporting Issues
 ## Reporting Issues
 If you encountered any bugs or issues following this tutorial please file a bug/issue here on [Github](https://github.com/meta-llama/llama-cookbook/issues)).
 If you encountered any bugs or issues following this tutorial please file a bug/issue here on [Github](https://github.com/meta-llama/llama-cookbook/issues)).

+ 0 - 5
getting-started/demo/ArticleSummarizer/app/src/main/java/com/example/llamaandroiddemo/BackendType.java

@@ -1,5 +0,0 @@
-package com.example.llamaandroiddemo;
-
-public enum BackendType {
-  XNNPACK
-}

+ 7 - 3
getting-started/demo/ArticleSummarizer/app/src/main/java/com/example/llamaandroiddemo/ExampleLlamaRemoteInference.kt

@@ -54,6 +54,10 @@ class ExampleLlamaRemoteInference(remoteURL: String) {
             .readTimeout(0, TimeUnit.MILLISECONDS) // No timeout for streaming
             .readTimeout(0, TimeUnit.MILLISECONDS) // No timeout for streaming
             .build()
             .build()
 
 
+        if (AppUtils.API_KEY == "") {
+            AppLogging.getInstance().log("API key not set, configure it in AppUtils")
+        }
+
         val request = Request.Builder()
         val request = Request.Builder()
             .url(url)
             .url(url)
             .addHeader("Authorization","Bearer " + AppUtils.API_KEY)
             .addHeader("Authorization","Bearer " + AppUtils.API_KEY)
@@ -64,7 +68,7 @@ class ExampleLlamaRemoteInference(remoteURL: String) {
         return client.newCall(request).execute()
         return client.newCall(request).execute()
     }
     }
 
 
-    private fun llamaChatCompletion(ctx: Context, modelName: String, conversationHistory: ArrayList<Message>, userProvidedSystemPrompt: String){
+    private fun llamaChatCompletion(ctx: Context, modelName: String, conversationHistory: ArrayList<Message>, userProvidedSystemPrompt: String, temperature: Double){
         var msg = """
         var msg = """
                         {
                         {
                           "role": "system",
                           "role": "system",
@@ -83,7 +87,7 @@ class ExampleLlamaRemoteInference(remoteURL: String) {
                     ],
                     ],
                     "model": "$modelName",
                     "model": "$modelName",
                     "repetition_penalty": 1,
                     "repetition_penalty": 1,
-                    "temperature": 0.6,
+                    "temperature": $temperature,
                     "top_p": 0.9,
                     "top_p": 0.9,
                     "max_completion_tokens": 2048,
                     "max_completion_tokens": 2048,
                     "stream": true
                     "stream": true
@@ -136,7 +140,7 @@ class ExampleLlamaRemoteInference(remoteURL: String) {
     //Example running simple inference + tool calls without using agent's workflow
     //Example running simple inference + tool calls without using agent's workflow
     private fun inferenceCallWithoutAgent(modelName: String, temperature: Double, conversationHistory: ArrayList<Message>, userProvidedSystemPrompt: String, ctx: Context, streaming: Boolean): String {
     private fun inferenceCallWithoutAgent(modelName: String, temperature: Double, conversationHistory: ArrayList<Message>, userProvidedSystemPrompt: String, ctx: Context, streaming: Boolean): String {
 
 
-        llamaChatCompletion(ctx,modelName,conversationHistory, userProvidedSystemPrompt)
+        llamaChatCompletion(ctx,modelName,conversationHistory, userProvidedSystemPrompt, temperature)
         return ""
         return ""
     }
     }
 
 

+ 0 - 3
getting-started/demo/ArticleSummarizer/app/src/main/java/com/example/llamaandroiddemo/HomescreenActivity.kt

@@ -3,14 +3,11 @@ package com.example.llamaandroiddemo
 import android.content.Intent
 import android.content.Intent
 import androidx.appcompat.app.AppCompatActivity
 import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
 import android.os.Bundle
-import android.view.View
 import android.widget.Button
 import android.widget.Button
-import android.widget.TextView
 
 
 class HomescreenActivity : AppCompatActivity() {
 class HomescreenActivity : AppCompatActivity() {
 
 
     private lateinit var startChatButton: Button
     private lateinit var startChatButton: Button
-    private lateinit var introTextView: TextView
 
 
     override fun onCreate(savedInstanceState: Bundle?) {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         super.onCreate(savedInstanceState)

+ 2 - 2
getting-started/demo/ArticleSummarizer/app/src/main/java/com/example/llamaandroiddemo/MainActivity.java

@@ -66,7 +66,6 @@ public class MainActivity extends AppCompatActivity implements Runnable, Inferen
   private LinearLayout mAddMediaLayout;
   private LinearLayout mAddMediaLayout;
   private static final int MAX_NUM_OF_IMAGES = 5;
   private static final int MAX_NUM_OF_IMAGES = 5;
   private static final int REQUEST_IMAGE_CAPTURE = 1;
   private static final int REQUEST_IMAGE_CAPTURE = 1;
-  private TextView mGenerationModeButton;
   private Uri cameraImageUri;
   private Uri cameraImageUri;
   private DemoSharedPreferences mDemoSharedPreferences;
   private DemoSharedPreferences mDemoSharedPreferences;
   private SettingsFields mCurrentSettingsFields;
   private SettingsFields mCurrentSettingsFields;
@@ -220,7 +219,7 @@ public class MainActivity extends AppCompatActivity implements Runnable, Inferen
 
 
   private void askUserToSelectModel() {
   private void askUserToSelectModel() {
     String askLoadModel =
     String askLoadModel =
-            "To get started, configure remote URL" +
+            "To get started, configure remote URL " +
             "from the top right corner";
             "from the top right corner";
     addSystemMessage(askLoadModel);
     addSystemMessage(askLoadModel);
   }
   }
@@ -508,6 +507,7 @@ public class MainActivity extends AppCompatActivity implements Runnable, Inferen
     systemPrompt += " Summarize and translate it into the following language: " + languageSelector.getSelectedLanguage();
     systemPrompt += " Summarize and translate it into the following language: " + languageSelector.getSelectedLanguage();
     Log.d("updated system prompt with language selected",systemPrompt);
     Log.d("updated system prompt with language selected",systemPrompt);
 
 
+    // User prompt is passed into the model as part of the getRecentSavedTextMessages
     result = exampleLlamaRemoteInference.inferenceStartWithoutAgent(
     result = exampleLlamaRemoteInference.inferenceStartWithoutAgent(
               modelName,
               modelName,
               temperature,
               temperature,

+ 1 - 52
getting-started/demo/ArticleSummarizer/app/src/main/java/com/example/llamaandroiddemo/SettingsFields.java

@@ -10,14 +10,6 @@ package com.example.llamaandroiddemo;
 
 
 public class SettingsFields {
 public class SettingsFields {
 
 
-  public String getModelFilePath() {
-    return modelFilePath;
-  }
-
-  public String getTokenizerFilePath() {
-    return tokenizerFilePath;
-  }
-
   public double getTemperature() {
   public double getTemperature() {
     return temperature;
     return temperature;
   }
   }
@@ -26,14 +18,6 @@ public class SettingsFields {
     return systemPrompt;
     return systemPrompt;
   }
   }
 
 
-  public ModelType getModelType() {
-    return modelType;
-  }
-
-  public BackendType getBackendType() {
-    return backendType;
-  }
-
   public String getRemoteURL() {
   public String getRemoteURL() {
     return remoteURL;
     return remoteURL;
   }
   }
@@ -50,62 +34,31 @@ public class SettingsFields {
     return remoteModel;
     return remoteModel;
   }
   }
 
 
-  private String modelFilePath;
-  private String tokenizerFilePath;
   private double temperature;
   private double temperature;
   private String systemPrompt;
   private String systemPrompt;
   private boolean isClearChatHistory;
   private boolean isClearChatHistory;
   private boolean isLoadModel;
   private boolean isLoadModel;
-  private ModelType modelType;
-  private BackendType backendType;
   private String remoteURL;
   private String remoteURL;
   private String remoteModel;
   private String remoteModel;
 
 
   public SettingsFields() {
   public SettingsFields() {
-    ModelType DEFAULT_MODEL = ModelType.LLAMA_3;
-    BackendType DEFAULT_BACKEND = BackendType.XNNPACK;
-
-    modelFilePath = "";
-    tokenizerFilePath = "";
     temperature = SettingsActivity.TEMPERATURE_MIN_VALUE;
     temperature = SettingsActivity.TEMPERATURE_MIN_VALUE;
     systemPrompt = "";
     systemPrompt = "";
     isClearChatHistory = false;
     isClearChatHistory = false;
     isLoadModel = false;
     isLoadModel = false;
-    modelType = DEFAULT_MODEL;
-    backendType = DEFAULT_BACKEND;
     remoteURL = "";
     remoteURL = "";
     remoteModel = "";
     remoteModel = "";
   }
   }
 
 
   public SettingsFields(SettingsFields settingsFields) {
   public SettingsFields(SettingsFields settingsFields) {
-    this.modelFilePath = settingsFields.modelFilePath;
-    this.tokenizerFilePath = settingsFields.tokenizerFilePath;
     this.temperature = settingsFields.temperature;
     this.temperature = settingsFields.temperature;
     this.systemPrompt = settingsFields.getSystemPrompt();
     this.systemPrompt = settingsFields.getSystemPrompt();
     this.isClearChatHistory = settingsFields.getIsClearChatHistory();
     this.isClearChatHistory = settingsFields.getIsClearChatHistory();
     this.isLoadModel = settingsFields.getIsLoadModel();
     this.isLoadModel = settingsFields.getIsLoadModel();
-    this.modelType = settingsFields.modelType;
-    this.backendType = settingsFields.backendType;
     this.remoteURL = settingsFields.remoteURL;
     this.remoteURL = settingsFields.remoteURL;
     this.remoteModel = settingsFields.remoteModel;
     this.remoteModel = settingsFields.remoteModel;
   }
   }
 
 
-  public void saveModelPath(String modelFilePath) {
-    this.modelFilePath = modelFilePath;
-  }
-
-  public void saveTokenizerPath(String tokenizerFilePath) {
-    this.tokenizerFilePath = tokenizerFilePath;
-  }
-
-  public void saveModelType(ModelType modelType) {
-    this.modelType = modelType;
-  }
-
-  public void saveBackendType(BackendType backendType) {
-    this.backendType = backendType;
-  }
-
   public void saveParameters(Double temperature) {
   public void saveParameters(Double temperature) {
     this.temperature = temperature;
     this.temperature = temperature;
   }
   }
@@ -132,14 +85,10 @@ public class SettingsFields {
 
 
   public boolean equals(SettingsFields anotherSettingsFields) {
   public boolean equals(SettingsFields anotherSettingsFields) {
     if (this == anotherSettingsFields) return true;
     if (this == anotherSettingsFields) return true;
-    return modelFilePath.equals(anotherSettingsFields.modelFilePath)
-                   && tokenizerFilePath.equals(anotherSettingsFields.tokenizerFilePath)
-                   && temperature == anotherSettingsFields.temperature
+    return temperature == anotherSettingsFields.temperature
                    && systemPrompt.equals(anotherSettingsFields.systemPrompt)
                    && systemPrompt.equals(anotherSettingsFields.systemPrompt)
                    && isClearChatHistory == anotherSettingsFields.isClearChatHistory
                    && isClearChatHistory == anotherSettingsFields.isClearChatHistory
                    && isLoadModel == anotherSettingsFields.isLoadModel
                    && isLoadModel == anotherSettingsFields.isLoadModel
-                   && modelType == anotherSettingsFields.modelType
-                   && backendType == anotherSettingsFields.backendType
                    && remoteURL.equals(anotherSettingsFields.remoteURL)
                    && remoteURL.equals(anotherSettingsFields.remoteURL)
                    && remoteModel.equals(anotherSettingsFields.remoteModel);
                    && remoteModel.equals(anotherSettingsFields.remoteModel);
   }
   }

BIN
getting-started/demo/ArticleSummarizer/app/src/main/res/drawable/logo.png