Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

openai_api.py 1.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  1. import openai
  2. import time
  3. import asyncio
  4. # Fill in your OpenAI setup params here
  5. openai.api_type = "azure"
  6. openai.api_key = '...'
  7. openai.api_base = 'https://example-endpoint.openai.azure.com/'
  8. openai.api_version = "2023-03-15-preview"
  9. DEPLOYMENT_ID="deployment-name"
  10. async def dispatch_openai_requests(
  11. deployment_id,
  12. messages_list,
  13. temperature,
  14. ):
  15. async_responses = [
  16. openai.ChatCompletion.acreate(
  17. deployment_id=deployment_id,
  18. messages=x,
  19. temperature=temperature,
  20. )
  21. for x in messages_list
  22. ]
  23. return await asyncio.gather(*async_responses)
  24. def call_async(samples, wrap_gen_message, print_result=False):
  25. message_list = []
  26. for sample in samples:
  27. input_msg = wrap_gen_message(sample)
  28. message_list.append(input_msg)
  29. try:
  30. predictions = asyncio.run(
  31. dispatch_openai_requests(
  32. deployment_id=DEPLOYMENT_ID,
  33. messages_list=message_list,
  34. temperature=0.0,
  35. )
  36. )
  37. except Exception as e:
  38. print(f"Error in call_async: {e}")
  39. time.sleep(6)
  40. return []
  41. results = []
  42. for sample, prediction in zip(samples, predictions):
  43. if prediction:
  44. if 'content' in prediction['choices'][0]['message']:
  45. sample['result'] = prediction['choices'][0]['message']['content']
  46. if print_result:
  47. print(sample['result'])
  48. results.append(sample)
  49. return results
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...