Skip to content

Commit

Permalink
Merge pull request #62 from Chainlit/brendan/fix-examples
Browse files Browse the repository at this point in the history
fix: Make examples work again
  • Loading branch information
willydouhard authored Apr 29, 2024
2 parents f18453a + cceaac0 commit eb30732
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 39 deletions.
37 changes: 18 additions & 19 deletions examples/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from dotenv import load_dotenv

from literalai import LiteralClient
from literalai.my_types import Attachment

load_dotenv()

Expand All @@ -12,45 +11,45 @@


async def main():
thread = await sdk.api.create_thread()
thread = sdk.api.create_thread()
step = sdk.start_step(name="test", thread_id=thread.id)
await sdk.api.send_steps([step])
sdk.api.send_steps([step])

try:
attachment = await sdk.api.create_attachment(
attachment=Attachment(
name="test",
url="https://www.perdu.com/",
mime="text/html",
thread_id=thread.id,
step_id=step.id,
),
attachment = sdk.api.create_attachment(
name="test",
url="https://www.perdu.com/",
mime="text/html",
thread_id=thread.id,
step_id=step.id,
)

print(attachment.to_dict())

attachment = await sdk.api.update_attachment(
attachment = sdk.api.update_attachment(
id=attachment.id,
url="https://api.github.com/repos/chainlit/chainlit",
mime="application/json",
metadata={"test": "test"},
update_params={
"url": "https://api.github.com/repos/chainlit/chainlit",
"mime": "application/json",
"metadata": {"test": "test"},
}
)

print(attachment.to_dict())

attachment = await sdk.api.get_attachment(id=attachment.id)
attachment = sdk.api.get_attachment(id=attachment.id)

print(attachment.to_dict())

await sdk.api.delete_attachment(id=attachment.id)
sdk.api.delete_attachment(id=attachment.id)

try:
attachment = await sdk.api.get_attachment(id=attachment.id)
attachment = sdk.api.get_attachment(id=attachment.id)
except Exception as e:
print(e)

finally:
await sdk.api.delete_thread(id=thread.id)
sdk.api.delete_thread(id=thread.id)


asyncio.run(main())
4 changes: 2 additions & 2 deletions examples/file-upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


async def main():
thread = await sdk.api.create_thread(metadata={"key": "value"}, tags=["hello"])
thread = sdk.api.create_thread(metadata={"key": "value"}, tags=["hello"])

id = thread.id
path = Path(__file__).parent / "./samplefile.txt"
Expand All @@ -22,7 +22,7 @@ async def main():
with open(path, "rb") as file:
data = file.read()

res = await sdk.api.upload_file(content=data, mime=mime, thread_id=id)
res = sdk.api.upload_file(content=data, mime=mime, thread_id=id)

print(res)

Expand Down
8 changes: 4 additions & 4 deletions examples/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_completion(welcome_message, text):
@sdk.thread
def run():
global thread_id
thread_id = sdk.get_current_thread_id()
thread_id = sdk.get_current_thread().id

welcome_message = "What's your name? "
sdk.message(content=welcome_message, type="system_message")
Expand All @@ -62,7 +62,7 @@ def run():
# Get the steps from the API for the demo
async def main():
print("\nSearching for the thread", thread_id, "...")
thread = await sdk.api.get_thread(id=thread_id)
thread = sdk.api.get_thread(id=thread_id)

print(json.dumps(thread.to_dict(), indent=2))

Expand All @@ -74,7 +74,7 @@ async def main():
return

# attach a score
await sdk.api.create_score(
sdk.api.create_score(
step_id=llm_step.id,
name="user-feedback",
type="HUMAN",
Expand All @@ -83,7 +83,7 @@ async def main():
)

# get the updated steps
thread = await sdk.api.get_thread(id=thread_id)
thread = sdk.api.get_thread(id=thread_id)

print(
json.dumps(
Expand Down
16 changes: 8 additions & 8 deletions examples/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@


async def main():
thread = await sdk.api.create_thread(metadata={"key": "value"}, tags=["hello"])
thread = sdk.api.create_thread(metadata={"key": "value"}, tags=["hello"])

id = thread.id
print(id, thread.to_dict())

thread = await sdk.api.update_thread(
thread = sdk.api.update_thread(
id=id,
metadata={"test": "test"},
tags=["hello:world"],
)

print(thread.to_dict())

thread = await sdk.api.get_thread(id=id)
thread = sdk.api.get_thread(id=id)

print(thread.to_dict())

await sdk.api.delete_thread(id=id)
sdk.api.delete_thread(id=id)

try:
thread = await sdk.api.get_thread(id=id)
thread = sdk.api.get_thread(id=id)
except Exception as e:
print(e)

after = None
max_calls = 5
while len((result := await sdk.api.get_threads(first=2, after=after)).data) > 0:
while len((result := sdk.api.get_threads(first=2, after=after)).data) > 0:
print(result.to_dict())
after = result.pageInfo.endCursor
max_calls -= 1
Expand All @@ -46,15 +46,15 @@ async def main():

print("filtered")

threads = await sdk.api.get_threads(
threads = sdk.api.get_threads(
filters=[
{
"field": "createdAt",
"operator": "gt",
"value": "2023-12-05",
},
],
order_by={"column": "participant", "direction": "ASC"},
# order_by={"column": "participant", "direction": "ASC"},
)
print(threads.to_dict())

Expand Down
12 changes: 6 additions & 6 deletions examples/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@


async def main():
user = await sdk.api.create_user(identifier="test-user", metadata={"name": "123"})
user = sdk.api.create_user(identifier="test-user-example", metadata={"name": "123"})

id = user.id
print(id, user.to_dict())

user = await sdk.api.update_user(
user = sdk.api.update_user(
id=id,
identifier="user",
metadata={"test": "test"},
)

print(user.to_dict())

user = await sdk.api.get_user(id=id)
user = sdk.api.get_user(id=id)

print(user.to_dict())

user = await sdk.api.get_user(identifier="user")
user = sdk.api.get_user(identifier="user")

print(user.to_dict())

await sdk.api.delete_user(id=id)
sdk.api.delete_user(id=id)

try:
user = await sdk.api.update_user(
user = sdk.api.update_user(
id=id,
identifier="user",
metadata={"test": "test"},
Expand Down

0 comments on commit eb30732

Please sign in to comment.