-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add feedback * chore: Add multi-threading test of call vars logic * feat: Remove langchain feedback related logic * feat: Remove leftover langchain items
- Loading branch information
1 parent
3f854d6
commit 5f82ad5
Showing
9 changed files
with
185 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import contextvars | ||
|
||
conversation_id = contextvars.ContextVar('conversation_id') | ||
ai_message_ids = contextvars.ContextVar('ai_message_ids') | ||
|
||
def get_ai_message_ids(response_id=None): | ||
if response_id is not None: | ||
#OpenAI | ||
return ai_message_ids.get({}).get(response_id, []) | ||
else: | ||
#Bedrock | ||
return ai_message_ids.get([]) | ||
|
||
def set_ai_message_ids(message_ids, response_id=None): | ||
if response_id is not None: | ||
#OpenAI | ||
current_ids = ai_message_ids.get({}) | ||
current_ids[response_id] = message_ids | ||
ai_message_ids.set(current_ids) | ||
else: | ||
#Bedrock | ||
ai_message_ids.set(message_ids) | ||
|
||
def set_conversation_id(id): | ||
conversation_id.set(id) | ||
|
||
def get_conversation_id(): | ||
return conversation_id.get(None) | ||
|
||
def create_ai_message_id(message_id, response_id=None): | ||
return { | ||
"conversation_id": get_conversation_id(), | ||
"response_id": response_id, | ||
"message_id": message_id, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pytest | ||
import sys | ||
import threading | ||
import time | ||
import asyncio | ||
import uuid | ||
|
||
from nr_openai_observability.call_vars import set_conversation_id, get_conversation_id | ||
|
||
failures = 0 | ||
|
||
def test_set_conversation_id(): | ||
global failures | ||
|
||
def set_conversation_id_thread(): | ||
global failures | ||
test_id = str(uuid.uuid4()) | ||
#Check that each new call gets a fresh context | ||
if get_conversation_id() is not None: | ||
failures += 1 | ||
set_conversation_id(test_id) | ||
time.sleep(0.001) | ||
#check that context hasn't been polluted with a different thread | ||
if test_id != get_conversation_id(): | ||
failures += 1 | ||
|
||
# Greatly improve the chance of an operation being interrupted | ||
# by thread switch. | ||
try: | ||
sys.setswitchinterval(1e-12) | ||
except AttributeError: | ||
# Python 2 compatible | ||
sys.setcheckinterval(1) | ||
|
||
threads = [] | ||
for _ in range(1000): | ||
t = threading.Thread(target=set_conversation_id_thread) | ||
threads.append(t) | ||
t.start() | ||
|
||
for thread in threads: | ||
thread.join() | ||
|
||
assert failures == 0 |