docstr: Compute perplexities from API call.
  
  :param observations:
      incoming observations
  :param results:
      results from API call
  
  :return ppls:
      return list of perplexities
docstr_tok: ["Compute","perplexities","from","API","call",".",":","param","observations",":","incoming","observations",":","param","results",":","results","from","API","call",":","return","ppls",":","return","list","of","perplexities"]
code: def compute_perplexities(
      observations: List[Message], results: List[Dict[str, Any]]
  ) -> Tuple[List[PPLMetric], List[PPLMetric]]:
      """
      Compute perplexities from API call.
  
      :param observations:
          incoming observations
      :param results:
          results from API call
  
      :return ppls:
          return list of perplexities
      """
      label_perplexities = []
      all_perplexities = []
      for obs, result in zip(observations, results):
          # need text offsets to figure out what comes from the prompt
          prompt_len = len(obs["prompt"])
  
          text_off = result["choices"][0]["logprobs"]["text_offset"]
          start_label = [
              i for i, off in enumerate(text_off) if off <= prompt_len
          ]
          assert len(start_label) > 0
          start_label = start_label[-1]
          all_log_probs = [
              lp
              for lp in result["choices"][0]["logprobs"][
                  "token_logprobs"
              ]
              if lp is not None
          ]
          if not all(l <= 0 for l in all_log_probs):
              logging.warning(
                  f"Out of {len(all_log_probs)} log probs, {len([l for l in all_log_probs if l > 0])} are > 0. "
                  "Clamping to 0"
              )
              all_log_probs = [min(l, 0) for l in all_log_probs]
  
          log_probs = all_log_probs[start_label:]
          loss = -sum(log_probs)
          label_perplexities.append(PPLMetric(loss, len(log_probs)))
          all_perplexities.append(
              PPLMetric(-sum(all_log_probs), len(all_log_probs))
          )
      return label_perplexities, all_perplexities
  
imports: import aiohttp
  import asyncio
  from enum import Enum
  import json
  import math
  from nltk.corpus import stopwords
  from nltk.tokenize import word_tokenize
  from nltk.stem import PorterStemmer
  import os
  import random
  import string
  import time
  from typing import List, Tuple, Optional, Dict, Any, Set, Union
  from parlai.agents.ir_baseline.ir_baseline import score_match, MaxPriorityQueue
  from parlai.core.dict import DictionaryAgent
  from parlai.core.message import Message
  from parlai.core.metrics import F1Metric
  from parlai.core.torch_generator_agent import PPLMetric
  import parlai.utils.logging
  import projects.bb3.constants
  import projects.bb3.prompts
  from projects.bb3.agents.module import Module
label_desc: Use image node_id 3 for calling the APIUtils obj's underlying member method code with example usage: obj.compute_perplexities(observations, results) and returns: label_perplexities,  all_perplexities