openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

Why are these fields private?

Open sammyjava opened this issue 2 years ago • 5 comments

I cannot access these fields because they are private in your package. But there are no getters, either. Is this a mistake? Or is there some other way I am supposed to access an Embedding? Right now it seems I need to make them public, or add getters, to be able to use them in my package.

/home/shokin/java/pure-gas/openai-pinecone/src/main/java/org/puregas/openai/EmbeddingTest.java:33: error: object is not public in Embedding; cannot be accessed from outside package
            System.out.println(embedding.object);
                                        ^
/home/shokin/java/pure-gas/openai-pinecone/src/main/java/org/puregas/openai/EmbeddingTest.java:34: error: index is not public in Embedding; cannot be accessed from outside package
            System.out.println(embedding.index);
                                        ^
/home/shokin/java/pure-gas/openai-pinecone/src/main/java/org/puregas/openai/EmbeddingTest.java:35: error: embedding is not public in Embedding; cannot be accessed from outside package
            System.out.println(embedding.embedding);

sammyjava avatar Mar 22 '23 01:03 sammyjava

I have noticed that Embedding.toString() is public and returns a string that I can parse, but that is hugely error-prone and clearly not optimal.

sammyjava avatar Mar 22 '23 01:03 sammyjava

Do you have lombok.jar installed in your dev environment? It should auto-magically generate all getters and setters.

Embedding.java does have object available.

https://github.com/TheoKanning/openai-java/blob/900e13bbda6c38adababebce8418f9aed0484d61/api/src/main/java/com/theokanning/openai/embedding/Embedding.java

If you have lombok installed and have cleaned and recompiled all the classes, you might have to Change the object serialized name as sometimes java reserved names conflict w/ variable names. Right before line 18 in Embedding.java....

    @SerializedName(value = "object")

cryptoapebot avatar Mar 22 '23 01:03 cryptoapebot

Okay, I've never used lombok so I was a bit mystified by this. I thought I could just include your packages, but realize your stuff is built under the lombok umbrella. That will be a bit of research on my part, I'm just using a text editor and the gradle compile. I didn't think an IDE was required. Could you document how lombok is required and what one needs to do to use it? Or perhaps a link? Thanks!

sammyjava avatar Mar 22 '23 01:03 sammyjava

GOT IT! These gradle packages allowed me to use Embedding.getIndex(), which of course is not present in the Javadoc. I've learned something, thanks for the tip.

    // https://mvnrepository.com/artifact/org.projectlombok/lombok
    compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.26'
    // https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit
    implementation group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.9.0'
    // https://mvnrepository.com/artifact/com.squareup.retrofit2/adapter-rxjava2
    implementation group: 'com.squareup.retrofit2', name: 'adapter-rxjava2', version: '2.9.0'
    // https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-jackson
    implementation group: 'com.squareup.retrofit2', name: 'converter-jackson', version: '2.9.0'

    // https://mvnrepository.com/artifact/com.theokanning.openai-gpt3-java/service
    implementation group: 'com.theokanning.openai-gpt3-java', name: 'service', version: '0.11.1'
    // https://mvnrepository.com/artifact/com.theokanning.openai-gpt3-java/client
    implementation group: 'com.theokanning.openai-gpt3-java', name: 'client', version: '0.11.1'
    // https://mvnrepository.com/artifact/com.theokanning.openai-gpt3-java/api
    implementation group: 'com.theokanning.openai-gpt3-java', name: 'api', version: '0.11.1'

sammyjava avatar Mar 22 '23 01:03 sammyjava

Well in theory it should just work!

Try calling:

String e = embedding.getObject();
Integer i = embedding.getIndex();
List<Double> le = embedding.getEmbedding();

Make sure you have lombok.java in your java classpath. In the data classes there are two things:

import lombok.Data;

@Data 
public class ... {
}

compiling it w/ gradle or not, it should just work. You should be able to declare:

List<Embedding> embeddings = service.createEmbeddings(embeddingRequest).getData();
for (Embedding e : embeddings) {
  System.out.println(e.getObject());
}

Lombok takes any declared variables and the data annotation will create the constructors, mappings, and getters/setters, equals, and toString with the default behaviors. Any of these can be overridden.

https://projectlombok.org/features/Data

cryptoapebot avatar Mar 22 '23 01:03 cryptoapebot