Skip to content

Instantly share code, notes, and snippets.

@transmissions11
Created March 2, 2023 18:12
Show Gist options
  • Save transmissions11/96828f42a0f05d0eddf342addde8e169 to your computer and use it in GitHub Desktop.
Save transmissions11/96828f42a0f05d0eddf342addde8e169 to your computer and use it in GitHub Desktop.
Reproduces a bug in the OpenAI Chat completions API when streaming w/ n>1
import openai
openai.api_key = "[REDACTED]"
responses = {}
for resp in openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Write a hash function in java."}],
n=5,
stream=True,
):
choice = resp.choices[0]
delta = choice.delta
# Populate the dict with an empty string if the key doesn't exist.
responses[choice.index] = responses.get(choice.index, "")
if hasattr(delta, "content"):
responses[choice.index] += delta.content
if choice.finish_reason is not None:
print("\n" + str(choice.index) + " finished due to: " + choice.finish_reason)
for i in range(0, len(responses)):
print(responses[i])
print("---------------")
print("\n\nObserve that some of the responses are cut off!")
@transmissions11
Copy link
Author

transmissions11 commented Mar 2, 2023

When running this you'll see one of two outputs (run many times and you should see both, generally #1 happens more often though):

1. Only one or two of the responses have reached a natural stopping point, while the rest are cut off prematurely.

0 finished due to: stop

1 finished due to: stop

2 finished due to: stop

3 finished due to: stop

4 finished due to: stop


As an AI language model, I can provide you with a sample implementation of a hash function in Java. However, please note that the actual implementation of a hash function depends on the specific use case and requirements. Here's an example implementation of a simple hash function in Java:

public static int hashCode(String key) {
    int hash = 0;
    for (int i = 0; i < key.length(); i++) {
        hash = (hash * 31) + key.charAt(i);
    }
    return hash;
}

This function takes a String as the input and returns an integer value as the hash code. The hash function uses a common algorithm which multiplies the existing hash value by a prime number (31
---------------


As an AI language model, I cannot implement code but can explain to you what a hash function is.

A hash function is a function that takes in input of arbitrary length and converts it into an output of fixed length. This output is usually a unique representation of the input. In Java, one common type of hash function is the hashCode() function, which is used to produce a hash code for an object.

The general structure of a hash function is as follows:

1. Define a fixed-length output for the hash function.
2. Take an input of arbitrary length.
3. Apply a series of operations to the input to transform it into the fixed-length output.
4. The output should be unique for each input.

Here is an example of a
---------------


As an AI language model, I can provide sample Java code for a hash function, however it is not considered secure for cryptographic purposes.

Here's an example of a simple hash function in Java:

public int hashFunction(String input){
    int hash = 0;
    for(int i = 0; i < input.length(); i++){
        hash += input.charAt(i);
    }
    return hash;
}

This hash function takes a string input and converts it into a hash value by adding up the integer codes of each character in the string. This is a very simple and insecure hash function, and should not be used for any critical applications. 

For more secure hash functions, it is recommended to use standard libraries such as Java's
---------------


As an AI language model, I am not capable of writing programs, but here is an example of a simple hash function in Java:

public class HashFunction {
   // hash function to map strings to integers
   public int hash(String key, int maxSize) {
      int hashCode = 0;
      for (int i = 0; i < key.length(); i++) {
         int letterValue = key.charAt(i) - 96;
         hashCode = ((hashCode << 5) + hashCode) + letterValue;
         hashCode %= maxSize;
      }
      return hashCode;
   }
}

This method takes in a key (a string) and a maxSize (the maximum size of the hash table). It loops through each
---------------


As an AI language model, I cannot write complete code programs. However, I can give you an example for a simple hash function that takes a string as input and returns an integer value:


public static int hash(String input) {
    int hashValue = 0;

    for (int i = 0; i < input.length(); i++) {
        hashValue += (int) input.charAt(i);
    }

    return hashValue;
}

This hash function sums the ASCII values of each character in the input string to create a hash value. It is a very basic hash function and is not very secure or collision-resistant. For practical applications, more complex hash functions like SHA-256 or MD5 should be used.
---------------


Observe that most of the responses are cut off (or empty!)

2. One response completes fully and the rest have not received any tokens before stopping.

0 finished due to: stop

1 finished due to: stop

2 finished due to: stop

3 finished due to: stop

4 finished due to: stop


As an AI language model, I can provide you with a sample implementation of a hash function in Java. Here's the code:

public class HashFunction {
    
    public static int hash(String s) {
        int hashVal = 0;
        for (int i = 0; i < s.length(); i++) {
            hashVal += s.charAt(i) * (i+1); // ASCII value of character * position
        }
        return hashVal;
    }
    
    public static void main(String[] args) {
        String input = "Hello World!";
        int hashVal = hash(input);
        System.out.println("Hash value of \"" + input + "\": " + hashVal);
    }
}

In this sample code, we define a `hash` method that takes a `String` parameter `s` and returns an integer hash value. We compute the hash value by iterating through each character of the string and multiplying its ASCII value by its position in the string. Finally, we return the sum of these products as the hash value.

We also provide a `main` method that demonstrates the use of the `hash` method. We create a `String` object `input` and compute its hash value using the `hash` method. We then print out a message that includes the original string and its hash value.

Note that this is just a simple example of a hash function implementation. In practice, hash functions can be much more complex depending on the specific use case and requirements.
---------------

---------------

---------------

---------------

---------------


Observe that most of the responses are cut off!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment