(
messages: List[Dict[str, str]],
temperature: int = 0,
model: str = "gpt-3.5-turbo",
scope: str = "USA",
purpose: str = "Generic",
session_id: str = None,
test_failure: bool = False,
# model: str = "gpt-4",
)
| 10 | |
| 11 | |
| 12 | def get_assistant_message_from_openai( |
| 13 | messages: List[Dict[str, str]], |
| 14 | temperature: int = 0, |
| 15 | model: str = "gpt-3.5-turbo", |
| 16 | scope: str = "USA", |
| 17 | purpose: str = "Generic", |
| 18 | session_id: str = None, |
| 19 | test_failure: bool = False, |
| 20 | # model: str = "gpt-4", |
| 21 | ): |
| 22 | # alright, it looks like gpt-3.5-turbo is ignoring the user messages in history |
| 23 | # let's go and re-create the chat in the last message! |
| 24 | final_payload = messages |
| 25 | |
| 26 | start = time.time() |
| 27 | try: |
| 28 | if test_failure: |
| 29 | raise Exception("Test failure") |
| 30 | res = openai.ChatCompletion.create( |
| 31 | model=model, |
| 32 | temperature=0, |
| 33 | messages=final_payload |
| 34 | ) |
| 35 | except Exception as e: |
| 36 | duration = time.time() - start |
| 37 | log_apicall( |
| 38 | duration, |
| 39 | 'openai', |
| 40 | model, |
| 41 | 0, |
| 42 | 0, |
| 43 | scope, |
| 44 | purpose, |
| 45 | session_id = session_id, |
| 46 | success=False, |
| 47 | log_message = str(e), |
| 48 | ) |
| 49 | raise e |
| 50 | duration = time.time() - start |
| 51 | |
| 52 | usage = res['usage'] |
| 53 | input_tokens = usage['prompt_tokens'] |
| 54 | output_tokens = usage['completion_tokens'] |
| 55 | |
| 56 | log_apicall( |
| 57 | duration, |
| 58 | 'openai', |
| 59 | model, |
| 60 | input_tokens, |
| 61 | output_tokens, |
| 62 | scope, |
| 63 | purpose, |
| 64 | session_id = session_id, |
| 65 | ) |
| 66 | |
| 67 | # completion = res['choices'][0]["message"]["content"] |
| 68 | assistant_message = res['choices'][0] |
| 69 |
no test coverage detected