Advertisement
Guest User

Untitled

a guest
Apr 26th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. # %%
  2. import random
  3. import openai
  4. import nest_asyncio
  5. import asyncio
  6. from pathlib import Path
  7. import json
  8.  
  9. nest_asyncio.apply()
  10. # %%
  11.  
  12.  
  13. def get_list():
  14.     return [random.randint(0, 10) for _ in range(10)]
  15.  
  16.  
  17. k = 32
  18.  
  19.  
  20. shot_template = """
  21. Unsorted list: {unsorted_list}
  22. Sorted list: {sorted_list}"""
  23.  
  24. few_shot_examples = [get_list() for _ in range(k)]
  25.  
  26. few_shot_prompt = "\n".join(
  27.     [shot_template.format(unsorted_list=shot, sorted_list=sorted(shot)) for shot in few_shot_examples]
  28. )
  29.  
  30. few_shot_template = (
  31.     few_shot_prompt
  32.     + """
  33.  
  34. Unsorted list: {unsorted_list}
  35. Sorted list:"""
  36. )
  37.  
  38. nothing_template = """
  39. Unsorted list: {unsorted_list}
  40. Sorted list:"""
  41.  
  42. python_template = """The sort function can be used to sort a list in ascending, descending or user defined
  43. order.
  44. To sort the list in ascending order, simply call list.sort(). This will sort a list
  45. of integers in ascending order so that the smallest integer will be first in the list
  46. and the largest integer will be the last.
  47. For example:
  48. list = {unsorted_list}
  49. list.sort() ="""
  50.  
  51.  
  52. async def run_prompt(prompt: str, model: str = "davinci-002"):
  53.     for _ in range(20):
  54.         try:
  55.             await asyncio.sleep(random.random() * 5)
  56.             response = await openai.Completion.acreate(
  57.                 engine=model,
  58.                 prompt=prompt,
  59.                 max_tokens=40,
  60.                 n=1,
  61.                 temperature=0.0,
  62.             )
  63.             return response.choices[0].text.split("\n")[0].strip()
  64.         except Exception as e:
  65.             # print(e)
  66.             continue
  67.     print(f"Failed to get response for {prompt}")
  68.     return ""
  69.  
  70.  
  71. templates = {
  72.     "Nothing": nothing_template,
  73.     "Few-shot": few_shot_template,
  74.     "Python": python_template,
  75. }
  76.  
  77.  
  78. async def run(model="davinci-002"):
  79.     lists = [get_list() for _ in range(200)]
  80.  
  81.     for name, template in templates.items():
  82.         answers = await asyncio.gather(*[run_prompt(template.format(unsorted_list=shot), model) for shot in lists])
  83.         correctness = [str(sorted(shot)) == ans for shot, ans in zip(lists, answers)]
  84.         acc = sum(correctness) / len(correctness)
  85.         print(f"{name} accuracy: {acc:.2f} +- {1.96 * (acc * (1 - acc) / len(correctness)) ** 0.5:.2f}")
  86.  
  87.  
  88. asyncio.run(run())
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement