ronantakizawa/codereview-bench huggingface dataset:
Repository: ollama/ollama
File: app/src/index.ts (TypeScript)

Code before:

  proc.stdout.on('data', data => {
    logger.info(data.toString().trim())
  })

  proc.stderr.on('data', data => {
    logger.error(data.toString().trim())
  })

  proc.on('exit', restart)
}

function restart() {
  setTimeout(server, 1000)
}

app.on('before-quit', () => {
  if (proc) {
    proc.off('exit', restart)
    proc.kill('SIGINT') // send SIGINT signal to the server, which also stops any loaded llms
  }
})

let currentReleaseURL = ''

async function checkNewRelease() {
  try {
    const response = await fetch('https://ollama.ai/api/update')

    if (!response.ok) {
      logger.error(`update server responded with ${response.status}`)
      return false
    }

    const data = await response.json()
    const fetchedURL = data.url

    const newRelease = currentReleaseURL === '' || currentReleaseURL !== fetchedURL
    currentReleaseURL = fetchedURL

    return newRelease
  } catch (error) {
    logger.error(`update check failed - ${error}`)
    return false
  }
}

async function checkUpdate() {
  const check = await checkNewRelease()
  if (check) {
    logger.info('update available')

Review comment (suggestion):
```suggestion
async function isNewReleaseAvailable() {
```

Code after:

  proc.stdout.on('data', data => {
    logger.info(data.toString().trim())
  })

  proc.stderr.on('data', data => {
    logger.error(data.toString().trim())
  })

  proc.on('exit', restart)
}

function restart() {
  setTimeout(server, 1000)
}

app.on('before-quit', () => {
  if (proc) {
    proc.off('exit', restart)
    proc.kill('SIGINT') // send SIGINT signal to the server, which also stops any loaded llms
  }
})

let currentReleaseURL = ''

async function isNewReleaseAvailable() {
  try {
    const response = await fetch('https://ollama.ai/api/update')

    if (response.status === 204) {
      return false
    }

    const data = await response.json()

    if (currentReleaseURL === data.url) {
      return false
    }

    currentReleaseURL = data.url
    return true
  } catch (error) {
    logger.error(`update check failed - ${error}`)
    return false
  }
}

async function checkUpdate() {
  const available = await isNewReleaseAvailable()
  if (available) {
    logger.info('checking for update')
Repository: ollama/ollama
File: server/images.go (Go)

Code before:
	}

	headers := make(http.Header)
	headers.Set("Content-Type", "application/vnd.docker.distribution.manifest.v2+json")
	resp, err := makeRequestWithRetry(ctx, "PUT", requestURL, headers, bytes.NewReader(manifestJSON), regOpts)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	fn(api.ProgressResponse{Status: "success"})

	return nil
}

func PullModel(ctx context.Context, name string, regOpts *RegistryOptions, fn func(api.ProgressResponse)) error {
	mp := ParseModelPath(name)

	var manifest *ManifestV2
	var err error
	var noprune string

	// build deleteMap to prune unused layers
	deleteMap := make(map[string]bool)

	if noprune := os.Getenv("OLLAMA_NOPRUNE"); noprune == "" {
		manifest, _, err = GetManifest(mp)
		if err != nil && !errors.Is(err, os.ErrNotExist) {
			return err
		}

		if manifest != nil {
			for _, l := range manifest.Layers {
				deleteMap[l.Digest] = true
			}
			deleteMap[manifest.Config.Digest] = true
		}
	}

	if mp.ProtocolScheme == "http" && !regOpts.Insecure {
		return fmt.Errorf("insecure protocol http")
	}

	fn(api.ProgressResponse{Status: "pulling manifest"})

	manifest, err = pullModelManifest(ctx, mp, regOpts)
	if err != nil {
		return fmt.Errorf("pull model manifest: %s", err)
	}

	var layers []*Layer

Review comment (suggestion):
I believe this is used later on in the function
```suggestion
	if noprune = os.Getenv("OLLAMA_NOPRUNE"); noprune == "" {
```

Code after:
	requestURL := mp.BaseURL()
	requestURL = requestURL.JoinPath("v2", mp.GetNamespaceRepository(), "manifests", mp.Tag)

	manifestJSON, err := json.Marshal(manifest)
	if err != nil {
		return err
	}

	headers := make(http.Header)
	headers.Set("Content-Type", "application/vnd.docker.distribution.manifest.v2+json")
	resp, err := makeRequestWithRetry(ctx, "PUT", requestURL, headers, bytes.NewReader(manifestJSON), regOpts)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	fn(api.ProgressResponse{Status: "success"})

	return nil
}

func PullModel(ctx context.Context, name string, regOpts *RegistryOptions, fn func(api.ProgressResponse)) error {
	mp := ParseModelPath(name)

	var manifest *ManifestV2
	var err error
	var noprune string

	// build deleteMap to prune unused layers
	deleteMap := make(map[string]bool)

	if noprune = os.Getenv("OLLAMA_NOPRUNE"); noprune == "" {
		manifest, _, err = GetManifest(mp)
		if err != nil && !errors.Is(err, os.ErrNotExist) {
			return err
		}

		if manifest != nil {
			for _, l := range manifest.Layers {
				deleteMap[l.Digest] = true
			}
			deleteMap[manifest.Config.Digest] = true
		}
	}

	if mp.ProtocolScheme == "http" && !regOpts.Insecure {
		return fmt.Errorf("insecure protocol http")
	}

	fn(api.ProgressResponse{Status: "pulling manifest"})
Repository: ollama/ollama
File: cmd/cmd.go (Go)

Code before:
			return errors.New("unexpected end to create model")
		}
	}

	return nil
}

func RunHandler(cmd *cobra.Command, args []string) error {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

	name := args[0]
	// check if the model exists on the server
	_, err = client.Show(context.Background(), &api.ShowRequest{Name: name})
	if err != nil {
		if apiErr, ok := err.(api.StatusError); ok && apiErr.StatusCode == 404 {
			// the model is not stored on the server, so we need to pull it
			if err := PullHandler(cmd, args); err != nil {
				return err
			}
		} else {
			return err
		}
	}

	return RunGenerate(cmd, args)
}

func PushHandler(cmd *cobra.Command, args []string) error {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

	insecure, err := cmd.Flags().GetBool("insecure")
	if err != nil {
		return err
	}

	var currentDigest string
	var bar *progressbar.ProgressBar

	request := api.PushRequest{Name: args[0], Insecure: insecure}
	fn := func(resp api.ProgressResponse) error {
		if resp.Digest != currentDigest && resp.Digest != "" {
			currentDigest = resp.Digest
			bar = progressbar.DefaultBytes(
				resp.Total,
				fmt.Sprintf("pushing %s...", resp.Digest[7:19]),

Review comment (suggestion):
I find this is easier to read and understand

```go
	var statusError api.StatusError
	switch {
	case errors.As(err, &statusError) && statusError.StatusCode == http.StatusNotFound:
		if err := PullHandler(cmd, args); err != nil {
			return err
		}
	case err != nil:
		return err
	}
```

Code after:
		if spinner.description != "success" {
			return errors.New("unexpected end to create model")
		}
	}

	return nil
}

func RunHandler(cmd *cobra.Command, args []string) error {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

	name := args[0]
	// check if the model exists on the server
	_, err = client.Show(context.Background(), &api.ShowRequest{Name: name})
	if err != nil {
		var statusError api.StatusError
		switch {
		case errors.As(err, &statusError) && statusError.StatusCode == http.StatusNotFound:
			if err := PullHandler(cmd, args); err != nil {
				return err
			}
		case err != nil:
			return err
		}
	}

	return RunGenerate(cmd, args)
}

func PushHandler(cmd *cobra.Command, args []string) error {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		return err
	}

	insecure, err := cmd.Flags().GetBool("insecure")
	if err != nil {
		return err
	}

	var currentDigest string
	var bar *progressbar.ProgressBar

	request := api.PushRequest{Name: args[0], Insecure: insecure}
	fn := func(resp api.ProgressResponse) error {
		if resp.Digest != currentDigest && resp.Digest != "" {
			currentDigest = resp.Digest
			bar = progressbar.DefaultBytes(
Repository: ollama/ollama
File: server/images.go (Go)

Code before:
		Reader: bytes.NewBuffer(configJSON),
	}
	return layer, nil
}

// GetSHA256Digest returns the SHA256 hash of a given buffer and returns it, and the size of buffer
func GetSHA256Digest(r io.Reader) (string, int64) {
	h := sha256.New()
	n, err := io.Copy(h, r)
	if err != nil {
		log.Fatal(err)
	}

	return fmt.Sprintf("sha256:%x", h.Sum(nil)), n
}

func makeRequestWithRetry(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.ReadSeeker, regOpts *RegistryOptions) (*http.Response, error) {
	for try := 0; try < maxRetries; try++ {
		resp, err := makeRequest(ctx, method, requestURL, headers, body, regOpts)
		if err != nil {
			log.Printf("couldn't start upload: %v", err)
			return nil, err
		}

		switch {
		case resp.StatusCode == http.StatusUnauthorized:
			auth := resp.Header.Get("www-authenticate")
			authRedir := ParseAuthRedirectString(auth)
			token, err := getAuthToken(ctx, authRedir)
			if err != nil {
				return nil, err
			}

			regOpts.Token = token
			if body != nil {
				if _, err := body.Seek(0, io.SeekStart); err != nil {
					return nil, err
				}
			}

			continue
		case resp.StatusCode >= http.StatusBadRequest:
			body, _ := io.ReadAll(resp.Body)
			return nil, fmt.Errorf("on upload registry responded with code %d: %s", resp.StatusCode, body)
		default:
			return resp, nil
		}
	}

	return nil, errMaxRetriesExceeded
}

Review comment (bug):
should we still log this error somewhere rather than ignoring it? I see that you want to prevent returning on error here.

Code after:
		log.Fatal(err)
	}

	return fmt.Sprintf("sha256:%x", h.Sum(nil)), n
}

func makeRequestWithRetry(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.ReadSeeker, regOpts *RegistryOptions) (*http.Response, error) {
	for try := 0; try < maxRetries; try++ {
		resp, err := makeRequest(ctx, method, requestURL, headers, body, regOpts)
		if err != nil {
			log.Printf("couldn't start upload: %v", err)
			return nil, err
		}

		switch {
		case resp.StatusCode == http.StatusUnauthorized:
			auth := resp.Header.Get("www-authenticate")
			authRedir := ParseAuthRedirectString(auth)
			token, err := getAuthToken(ctx, authRedir)
			if err != nil {
				return nil, err
			}

			regOpts.Token = token
			if body != nil {
				body.Seek(0, io.SeekStart)
			}

			continue
		case resp.StatusCode == http.StatusNotFound:
			return nil, os.ErrNotExist
		case resp.StatusCode >= http.StatusBadRequest:
			body, err := io.ReadAll(resp.Body)
			if err != nil {
				return nil, fmt.Errorf("%d: %s", resp.StatusCode, err)
			}

			return nil, fmt.Errorf("%d: %s", resp.StatusCode, body)
		default:
			return resp, nil
		}
	}

	return nil, errMaxRetriesExceeded
}

func makeRequest(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.Reader, regOpts *RegistryOptions) (*http.Response, error) {
	if requestURL.Scheme != "http" && regOpts != nil && regOpts.Insecure {
		requestURL.Scheme = "http"
	}
Repository: ollama/ollama
File: examples/python-loganalysis/loganalysis.py (Python)

Code before:
    end_index = min(len(log_lines), error_line + postlines)
    error_logs.extend(log_lines[start_index:end_index])

  return error_logs

error_logs = find_errors_in_log_file()

data = {
  "prompt": "\n".join(error_logs), 
  "model": "mattw/loganalyzer"
}


response = requests.post("http://localhost:11434/api/generate", json=data, stream=True)
for line in response.iter_lines():
  if line:
    json_data = json.loads(line)
    if json_data['done'] == False:
      print(json_data['response'], end='', flush=True)









Review comment (suggestion):
We should remove all these new lines at the end
```suggestion
```

Code after:
import sys
import re
import requests
import json

# prelines and postlines represent the number of lines of context to include in the output around the error
prelines = 10
postlines = 10

def find_errors_in_log_file():
  if len(sys.argv) < 2:
    print("Usage: python loganalysis.py <filename>")
    return

  log_file_path = sys.argv[1]
  with open(log_file_path, 'r') as log_file:
    log_lines = log_file.readlines()

error_logs = []
    for i, line in enumerate(log_lines):
        if "error" in line.lower():
            start_index = max(0, i - prelines)
            end_index = min(len(log_lines), i + postlines + 1)
            error_logs.extend(log_lines[start_index:end_index])

  return error_logs

error_logs = find_errors_in_log_file()

data = {
  "prompt": "\n".join(error_logs), 
  "model": "mattw/loganalyzer"
}


response = requests.post("http://localhost:11434/api/generate", json=data, stream=True)
for line in response.iter_lines():
  if line:
    json_data = json.loads(line)
    if json_data['done'] == False:
      print(json_data['response'], end='', flush=True)
Repository: ollama/ollama
File: examples/python-loganalysis/loganalysis.py (Python)

Code before:
import sys
import re
import requests
import json

prelines = 10
postlines = 10

def find_errors_in_log_file():
  if len(sys.argv) < 2:
    print("Usage: python loganalysis.py <filename>")
    return

  log_file_path = sys.argv[1]
  with open(log_file_path, 'r') as log_file:
    log_lines = log_file.readlines()

  error_lines = []
  for i, line in enumerate(log_lines):
    if re.search('error', line, re.IGNORECASE):
      error_lines.append(i)

  error_logs = []
  for error_line in error_lines:
    start_index = max(0, error_line - prelines)
    end_index = min(len(log_lines), error_line + postlines)
    error_logs.extend(log_lines[start_index:end_index])

  return error_logs

error_logs = find_errors_in_log_file()


Review comment (suggestion):
```suggestion
# prelines and postlines represent the number of lines of context to include in the output around the error
prelines = 10
```

Code after:
import sys
import re
import requests
import json

# prelines and postlines represent the number of lines of context to include in the output around the error
prelines = 10
postlines = 10

def find_errors_in_log_file():
  if len(sys.argv) < 2:
    print("Usage: python loganalysis.py <filename>")
    return

  log_file_path = sys.argv[1]
  with open(log_file_path, 'r') as log_file:
    log_lines = log_file.readlines()

error_logs = []
    for i, line in enumerate(log_lines):
        if "error" in line.lower():
            start_index = max(0, i - prelines)
            end_index = min(len(log_lines), i + postlines + 1)
            error_logs.extend(log_lines[start_index:end_index])

  return error_logs

error_logs = find_errors_in_log_file()

data = {
  "prompt": "\n".join(error_logs), 
  "model": "mattw/loganalyzer"
Repository: ollama/ollama
File: examples/python-loganalysis/loganalysis.py (Python)

Code before:
import re
import requests
import json

prelines = 10
postlines = 10

def find_errors_in_log_file():
  if len(sys.argv) < 2:
    print("Usage: python loganalysis.py <filename>")
    return

  log_file_path = sys.argv[1]
  with open(log_file_path, 'r') as log_file:
    log_lines = log_file.readlines()

  error_lines = []
  for i, line in enumerate(log_lines):
    if re.search('error', line, re.IGNORECASE):
      error_lines.append(i)

  error_logs = []
  for error_line in error_lines:
    start_index = max(0, error_line - prelines)
    end_index = min(len(log_lines), error_line + postlines)
    error_logs.extend(log_lines[start_index:end_index])

  return error_logs

error_logs = find_errors_in_log_file()

data = {
  "prompt": "\n".join(error_logs), 
  "model": "mattw/loganalyzer"
}


response = requests.post("http://localhost:11434/api/generate", json=data, stream=True)
for line in response.iter_lines():
  if line:
    json_data = json.loads(line)
    if json_data['done'] == False:
      print(json_data['response'], end='', flush=True)









Review comment (suggestion):
Just a suggestion, I think you could do this all in one loop

```suggestion
error_logs = []
    for i, line in enumerate(log_lines):
        if "error" in line.lower():
            start_index = max(0, i - prelines)
            end_index = min(len(log_lines), i + postlines + 1)
            error_logs.extend(log_lines[start_index:end_index])
```

Code after:
import re
import requests
import json

# prelines and postlines represent the number of lines of context to include in the output around the error
prelines = 10
postlines = 10

def find_errors_in_log_file():
  if len(sys.argv) < 2:
    print("Usage: python loganalysis.py <filename>")
    return

  log_file_path = sys.argv[1]
  with open(log_file_path, 'r') as log_file:
    log_lines = log_file.readlines()

error_logs = []
    for i, line in enumerate(log_lines):
        if "error" in line.lower():
            start_index = max(0, i - prelines)
            end_index = min(len(log_lines), i + postlines + 1)
            error_logs.extend(log_lines[start_index:end_index])

  return error_logs

error_logs = find_errors_in_log_file()

data = {
  "prompt": "\n".join(error_logs), 
  "model": "mattw/loganalyzer"
}


response = requests.post("http://localhost:11434/api/generate", json=data, stream=True)
for line in response.iter_lines():
  if line:
    json_data = json.loads(line)
    if json_data['done'] == False:
      print(json_data['response'], end='', flush=True)
Repository: ollama/ollama
File: examples/python-json-datagenerator/predefinedschema.py (Python)

Code before:
import requests
import json
import random

model = "llama2"
template = {
  "firstName": "", 
  "lastName": "", 
  "address": {
    "theStreet": "", 
    "theCity": "", 
    "theState": "", 
    "theZipCode": ""
  }, 
  "phoneNumber": ""
}

prompt = f"generate one realisticly believable sample data set of a persons first name, last name, address in the US, and  phone number. \nUse the following template: {json.dumps(template)}."

data = {
    "prompt": prompt,
    "model": model,
    "format": "json",
    "stream": False,
    "options": {"temperature": 2.5, "top_p": 0.99, "top_k": 100},
}

print(f"Generating a sample user")
response = requests.post("http://localhost:11434/api/generate", json=data, stream=False)
json_data = json.loads(response.text)
print(json.dumps(json.loads(json_data["response"]), indent=2))


Review comment (suggestion):
Just a suggestion
```suggestion
    "street": "", 
    "city": "", 
    "state": "", 
    "zipCode": ""
```

Code after:
import requests
import json
import random

model = "llama2"
template = {
  "firstName": "", 
  "lastName": "", 
  "address": {
    "street": "", 
    "city": "", 
    "state": "", 
    "zipCode": ""
  }, 
  "phoneNumber": ""
}

prompt = f"generate one realistically believable sample data set of a persons first name, last name, address in the US, and  phone number. \nUse the following template: {json.dumps(template)}."

data = {
    "prompt": prompt,
    "model": model,
    "format": "json",
    "stream": False,
    "options": {"temperature": 2.5, "top_p": 0.99, "top_k": 100},
}

print(f"Generating a sample user")
response = requests.post("http://localhost:11434/api/generate", json=data, stream=False)
json_data = json.loads(response.text)
print(json.dumps(json.loads(json_data["response"]), indent=2))
Repository: ollama/ollama
File: examples/python-json-datagenerator/predefinedschema.py (Python)

Code before:
import requests
import json
import random

model = "llama2"
template = {
  "firstName": "", 
  "lastName": "", 
  "address": {
    "theStreet": "", 
    "theCity": "", 
    "theState": "", 
    "theZipCode": ""
  }, 
  "phoneNumber": ""
}

prompt = f"generate one realisticly believable sample data set of a persons first name, last name, address in the US, and  phone number. \nUse the following template: {json.dumps(template)}."

data = {
    "prompt": prompt,
    "model": model,
    "format": "json",
    "stream": False,
    "options": {"temperature": 2.5, "top_p": 0.99, "top_k": 100},
}

print(f"Generating a sample user")
response = requests.post("http://localhost:11434/api/generate", json=data, stream=False)
json_data = json.loads(response.text)
print(json.dumps(json.loads(json_data["response"]), indent=2))


Review comment (suggestion):
```suggestion
prompt = f"generate one realistically believable sample data set of a persons first name, last name, address in the US, and  phone number. \nUse the following template: {json.dumps(template)}."
```

Code after:
import requests
import json
import random

model = "llama2"
template = {
  "firstName": "", 
  "lastName": "", 
  "address": {
    "street": "", 
    "city": "", 
    "state": "", 
    "zipCode": ""
  }, 
  "phoneNumber": ""
}

prompt = f"generate one realistically believable sample data set of a persons first name, last name, address in the US, and  phone number. \nUse the following template: {json.dumps(template)}."

data = {
    "prompt": prompt,
    "model": model,
    "format": "json",
    "stream": False,
    "options": {"temperature": 2.5, "top_p": 0.99, "top_k": 100},
}

print(f"Generating a sample user")
response = requests.post("http://localhost:11434/api/generate", json=data, stream=False)
json_data = json.loads(response.text)
print(json.dumps(json.loads(json_data["response"]), indent=2))
Repository: ollama/ollama
File: examples/python-json-datagenerator/randomaddresses.py (Python)

Code before:
import requests
import json
import random

countries = [
    "the US",
    "the UK",
    "the Netherlands",
    "Germany",
    "Mexico",
    "Canada",
    "France",
]
country = random.choice(countries)
model = "llama2"

prompt = f"generate one realisticly believable sample data set of a persons first name, last name, address in {country}, and  phone number. Do not use common names. Respond using JSON. Key names should with no backslashes, values should use plain ascii with no special characters."

data = {
    "prompt": prompt,
    "model": model,
    "format": "json",
    "stream": False,
    "options": {"temperature": 2.5, "top_p": 0.99, "top_k": 100},
}

print(f"Generating a sample user in {country}")
response = requests.post("http://localhost:11434/api/generate", json=data, stream=False)
json_data = json.loads(response.text)

print(json.dumps(json.loads(json_data["response"]), indent=2))


Review comment (suggestion):
```suggestion
prompt = f"generate one realistically believable sample data set of a persons first name, last name, address in {country}, and phone number. Do not use common names. Respond using JSON. Key names should have no backslashes, values should use plain ascii with no special characters."
```

Code after:
import requests
import json
import random

countries = [
    "United States",
    "United Kingdom",
    "the Netherlands",
    "Germany",
    "Mexico",
    "Canada",
    "France",
]
country = random.choice(countries)
model = "llama2"

prompt = f"generate one realistically believable sample data set of a persons first name, last name, address in {country}, and phone number. Do not use common names. Respond using JSON. Key names should have no backslashes, values should use plain ascii with no special characters."

data = {
    "prompt": prompt,
    "model": model,
    "format": "json",
    "stream": False,
    "options": {"temperature": 2.5, "top_p": 0.99, "top_k": 100},
}

print(f"Generating a sample user in {country}")
response = requests.post("http://localhost:11434/api/generate", json=data, stream=False)
json_data = json.loads(response.text)

print(json.dumps(json.loads(json_data["response"]), indent=2))