Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
isahers1 committed Jan 15, 2025
1 parent f57b38c commit d3494df
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
27 changes: 16 additions & 11 deletions docs/evaluation/how_to_guides/evaluate_with_attachments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ All of the below features are available in the following SDK versions:
<CodeTabs
tabs={[
PythonBlock(`import requests
import uuid\n
import uuid
from pathlib import Path\n
from langsmith import Client
from langsmith.schemas import ExampleUploadWithAttachments, Attachment\n
# Publicly available test files
pdf_url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
wav_url = "https://openaiassets.blob.core.windows.net/$web/API/docs/audio/alloy.wav"
png_url = "https://www.w3.org/Graphics/PNG/nurbcup2si.png"\n
wav_url = "https://openaiassets.blob.core.windows.net/$web/API/docs/audio/alloy.wav"\n
# Fetch the files as bytes\n
pdf_bytes = requests.get(pdf_url).content
wav_bytes = requests.get(wav_url).content
png_bytes = requests.get(png_url).content\n
wav_bytes = requests.get(wav_url).content\n
# Define the LANGCHAIN_API_KEY environment variable with your API key
langsmith_client = Client()\n
dataset_name = "attachment-test-dataset:" + str(uuid.uuid4())[0:8]\n
Expand All @@ -70,20 +71,22 @@ example = ExampleUploadWithAttachments(
},
attachments={
"my_pdf": ("application/pdf", pdf_bytes),
"my_wav": ("audio/wav", wav_bytes),
"my_img": Attachment(mime_type="image/png", data=png_bytes)
"my_wav": Attachment(mime_type="audio/wav", data=wav_bytes),
"my_img": ("image/png", Path(__file__).parent / "my_img.png")
},
)\n
# Upload the examples with attachments
langsmith_client.upload_examples_multipart(dataset_id=dataset.id, uploads=[example])
# Must pass the dangerously_allow_filesystem flag to allow file paths
langsmith_client.upload_examples_multipart(dataset_id=dataset.id, uploads=[example], dangerously_allow_filesystem=True)
`,
`In the Python SDK, you can use the \`upload_examples_multipart\` method to upload examples with attachments.\n
Note that this is a different method from the standard \`create_examples\` method, which currently not support attachments.\n
Utilize the \`ExampleUploadWithAttachments\` type to define examples with attachments.\n
Each \`Attachment\` requires:
- \`mime_type\` (str): The MIME type of the file (e.g., \`"image/png"\`).
- \`data\` (bytes): The binary content of the file.\n
You can also define an attachment with a tuple tuple of the form \`(mime_type, data)\` for convenience.
- \`data\` (bytes | Path): The binary content of the file, or the file path.\n
You can also define an attachment with a tuple tuple of the form \`(mime_type, data)\` for convenience. \n
Note that to use the file path instead of the raw bytes, you need to set the \`dangerously_allow_filesystem\` option to \`True\`.\n
`
),
TypeScriptBlock(`import { Client } from "langsmith";
Expand Down Expand Up @@ -147,7 +150,9 @@ Note that this is a different method from the standard \`createExamples\` method
Each attachment requires either a \`Uint8Array\` or an \`ArrayBuffer\` as the data type.\n
- \`Uint8Array\`: Useful for handling binary data directly.
- \`ArrayBuffer\`: Represents fixed-length binary data, which can be converted to \`Uint8Array\` as needed.\n`),
- \`ArrayBuffer\`: Represents fixed-length binary data, which can be converted to \`Uint8Array\` as needed.\n
Note that you cannot directly pass in a file path in the TypeScript SDK, as accessing local files is not supported in all runtime environments.\n
`),
]}
groupId="client-language"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ This guide explains how to define and trace attachments using the `Attachment` t
tabs={[
PythonBlock(
`from langsmith import traceable
from langsmith.schemas import Attachment\n\n
@traceable
from langsmith.schemas import Attachment
from pathlib import Path
import os\n\n
# Must set dangerously_allow_filesystem to True if you want to use file paths
@traceable(dangerously_allow_filesystem=True)
def trace_with_attachments(
val: int,
text: str,
Expand All @@ -53,12 +56,11 @@ image_data = load_file("my_image.png")
audio_data = load_file("my_mp3.mp3")
video_data = load_file("my_video.mp4")
pdf_data = load_file("my_document.pdf")
csv_data = load_file("my_csv.csv")
image_attachment = Attachment(mime_type="image/png", data=image_data)
audio_attachment = Attachment(mime_type="audio/mpeg", data=audio_data)
video_attachment = Attachment(mime_type="video/mp4", data=video_data)
pdf_attachment = Attachment(mime_type="application/pdf", data=pdf_data)
csv_attachment = Attachment(mime_type="text/csv", data=csv_data)
pdf_attachment = ("application/pdf", pdf_data) # Can just define as tuple of (mime_type, data)
csv_attachment = Attachment(mime_type="text/csv", data=Path(os.getcwd()) / "my_csv.csv")
# Define other parameters
Expand All @@ -80,8 +82,10 @@ csv=csv_attachment,
Each \`Attachment\` requires:\n
- \`mime_type\` (str): The MIME type of the file (e.g., \`"image/png"\`).
- \`data\` (bytes): The binary content of the file.\n
Simply decorate a function with \`@traceable\` and include your \`Attachment\` instances as arguments.`),
- \`data\` (bytes | Path): The binary content of the file, or the file path.\n
You can also define an attachment with a tuple tuple of the form \`(mime_type, data)\` for convenience. \n
Simply decorate a function with \`@traceable\` and include your \`Attachment\` instances as arguments.
Note that to use the file path instead of the raw bytes, you need to set the \`dangerously_allow_filesystem\` flag to \`True\` in your traceable decorator.\n`),
TypeScriptBlock(`import { traceable } from "langsmith/traceable";\n
const traceableWithAttachments = traceable(
(
Expand Down Expand Up @@ -135,6 +139,7 @@ TypeScriptBlock(`import { traceable } from "langsmith/traceable";\n
- \`ArrayBuffer\`: Represents fixed-length binary data, which can be converted to \`Uint8Array\` as needed.\n
Wrap your function with \`traceable\` and include your attachments within the \`extractAttachments\` option.\n
In the TypeScript SDK, the \`extractAttachments\` function is an optional parameter in the \`traceable\` configuration. When the traceable-wrapped function is invoked, it extracts binary data (e.g., images, audio files) from your inputs and logs them alongside other trace data, specifying their MIME types.\n
Note that you cannot directly pass in a file path in the TypeScript SDK, as accessing local files is not supported in all runtime environments.\n
\`\`\`
type AttachmentData = Uint8Array | ArrayBuffer;
type Attachments = Record<string, [string, AttachmentData]>;\n
Expand Down

0 comments on commit d3494df

Please sign in to comment.