Skip to content

Commit a76029c

Browse files
committed
Anthropic - files and skills examples
1 parent 49e1627 commit a76029c

15 files changed

+1072
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.cequence.openaiscala.examples.anthropic.files
2+
3+
import io.cequence.openaiscala.anthropic.service.{AnthropicService, AnthropicServiceFactory}
4+
import io.cequence.openaiscala.examples.ExampleBase
5+
6+
import java.io.File
7+
import scala.concurrent.Future
8+
9+
// requires `openai-scala-anthropic-client` as a dependency and `ANTHROPIC_API_KEY` and `EXAMPLE_FILE_PATH` environment variables to be set
10+
object AnthropicCreateFile extends ExampleBase[AnthropicService] {
11+
12+
override protected val service: AnthropicService = AnthropicServiceFactory()
13+
14+
override protected def run: Future[_] = {
15+
println("=" * 60)
16+
println("Creating (uploading) a file")
17+
println("=" * 60)
18+
println()
19+
20+
// Read file path from environment variable
21+
val filePathOpt = sys.env.get("EXAMPLE_FILE_PATH")
22+
23+
filePathOpt match {
24+
case None =>
25+
println("Error: EXAMPLE_FILE_PATH environment variable not set")
26+
println("Please set it to the path of the file you want to upload")
27+
println("Example: export EXAMPLE_FILE_PATH=/path/to/document.pdf")
28+
Future.successful(())
29+
30+
case Some(filePath) =>
31+
val file = new File(filePath)
32+
33+
if (!file.exists()) {
34+
println(s"Error: File does not exist: $filePath")
35+
println("Please check the EXAMPLE_FILE_PATH environment variable")
36+
Future.successful(())
37+
} else {
38+
// Upload file with optional custom filename
39+
service.createFile(file, None).map { fileMetadata =>
40+
println(s"File uploaded successfully!")
41+
println(s" File ID: ${fileMetadata.id}")
42+
println(s" Filename: ${fileMetadata.filename}")
43+
println(s" MIME type: ${fileMetadata.mimeType}")
44+
println(s" Size: ${fileMetadata.sizeBytes} bytes")
45+
println(s" Created: ${fileMetadata.createdAt}")
46+
println(s" Downloadable: ${fileMetadata.downloadable}")
47+
println()
48+
println("=" * 60)
49+
println("Note: Save the File ID to use in other operations")
50+
println("Note: Set EXAMPLE_FILE_NAME to use a custom filename")
51+
println("Note: User-uploaded files are always non-downloadable")
52+
println("=" * 60)
53+
}
54+
}
55+
}
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.cequence.openaiscala.examples.anthropic.files
2+
3+
import io.cequence.openaiscala.anthropic.service.{AnthropicService, AnthropicServiceFactory}
4+
import io.cequence.openaiscala.examples.ExampleBase
5+
6+
import scala.concurrent.Future
7+
8+
// requires `openai-scala-anthropic-client` as a dependency and `ANTHROPIC_API_KEY` environment variable to be set
9+
object AnthropicDeleteFile extends ExampleBase[AnthropicService] {
10+
11+
override protected val service: AnthropicService = AnthropicServiceFactory()
12+
13+
override protected def run: Future[_] = {
14+
println("=" * 60)
15+
println("Deleting file")
16+
println("=" * 60)
17+
println()
18+
19+
// IMPORTANT: Replace this with an actual file ID from your workspace
20+
// You can get file IDs by running AnthropicListFiles
21+
val fileId = "file_01..."
22+
23+
println(s"Attempting to delete file: $fileId")
24+
println()
25+
26+
service
27+
.deleteFile(fileId)
28+
.map { response =>
29+
println(s"File deleted successfully!")
30+
println(s" Deleted file ID: ${response.id}")
31+
println(s" Type: ${response.`type`}")
32+
println()
33+
println("=" * 60)
34+
println("Note: Deleted files cannot be recovered")
35+
println("Note: The file is now inaccessible through the API")
36+
println("=" * 60)
37+
}
38+
.recover {
39+
case ex if ex.getMessage.contains("not_found") =>
40+
println(s"File not found: $fileId")
41+
println("The file may have already been deleted")
42+
println()
43+
println("=" * 60)
44+
println("Tips:")
45+
println(" - Use AnthropicListFiles to see available files")
46+
println(" - Use AnthropicGetFileMetadata to check if file exists")
47+
println("=" * 60)
48+
49+
case ex =>
50+
println(s"Error deleting file: ${ex.getMessage}")
51+
println()
52+
println("=" * 60)
53+
println("Note: Check the error message for details")
54+
println("=" * 60)
55+
}
56+
}
57+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.cequence.openaiscala.examples.anthropic.files
2+
3+
import akka.stream.scaladsl.FileIO
4+
import io.cequence.openaiscala.anthropic.service.{AnthropicService, AnthropicServiceFactory}
5+
import io.cequence.openaiscala.examples.ExampleBase
6+
7+
import java.nio.file.Paths
8+
import scala.concurrent.Future
9+
10+
// requires `openai-scala-anthropic-client` as a dependency and `ANTHROPIC_API_KEY` environment variable to be set
11+
object AnthropicDownloadFile extends ExampleBase[AnthropicService] {
12+
13+
override protected val service: AnthropicService = AnthropicServiceFactory()
14+
15+
// IMPORTANT: Replace this with an actual file ID from your workspace
16+
// You can get file IDs by running AnthropicListFiles
17+
private val fileId = "file_01..."
18+
19+
override protected def run: Future[_] = {
20+
println("=" * 60)
21+
println("Downloading file")
22+
println("=" * 60)
23+
println()
24+
25+
// Expand home directory properly (~ doesn't work with Paths.get)
26+
val homeDir = System.getProperty("user.home")
27+
28+
for {
29+
// First, get file metadata to retrieve the actual filename
30+
metadataOpt <- service.getFileMetadata(fileId)
31+
32+
// Then download the file
33+
sourceOpt <- service.downloadFile(fileId)
34+
35+
result <- (metadataOpt, sourceOpt) match {
36+
case (Some(metadata), Some(source)) =>
37+
val outputPath = Paths.get(homeDir, "Downloads", metadata.filename)
38+
39+
println(s"Downloading file: $fileId")
40+
println(s" Filename: ${metadata.filename}")
41+
println(s" Size: ${metadata.sizeBytes} bytes")
42+
println(s" MIME type: ${metadata.mimeType}")
43+
println(s"Saving to: $outputPath")
44+
println()
45+
46+
// Stream the file contents to disk
47+
source
48+
.runWith(FileIO.toPath(outputPath))
49+
.map { ioResult =>
50+
println(s"Download complete!")
51+
println(s" Bytes written: ${ioResult.count}")
52+
println(s" Saved to: $outputPath")
53+
println()
54+
println("=" * 60)
55+
println("Note: Large files are streamed efficiently")
56+
println("=" * 60)
57+
}
58+
.recover { case ex =>
59+
println(s"Error downloading file: ${ex.getMessage}")
60+
}
61+
62+
case (None, _) =>
63+
println(s"File not found: $fileId")
64+
println()
65+
println("=" * 60)
66+
println("Tips:")
67+
println(" - The file may have been deleted")
68+
println(" - Check the file ID is correct")
69+
println(" - Use AnthropicListFiles to see available files")
70+
println("=" * 60)
71+
Future.successful(())
72+
73+
case (_, None) =>
74+
println(s"File content not available for: $fileId")
75+
println("Note: Only Claude-generated files are downloadable")
76+
Future.successful(())
77+
}
78+
} yield result
79+
}
80+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.cequence.openaiscala.examples.anthropic.files
2+
3+
import io.cequence.openaiscala.anthropic.service.{AnthropicService, AnthropicServiceFactory}
4+
import io.cequence.openaiscala.examples.ExampleBase
5+
6+
import scala.concurrent.Future
7+
8+
// requires `openai-scala-anthropic-client` as a dependency and `ANTHROPIC_API_KEY` environment variable to be set
9+
object AnthropicGetFileMetadata extends ExampleBase[AnthropicService] {
10+
11+
override protected val service: AnthropicService = AnthropicServiceFactory()
12+
13+
// IMPORTANT: Replace this with an actual file ID from your workspace
14+
// You can get file IDs by running AnthropicListFiles
15+
val fileId = "file_01..."
16+
17+
override protected def run: Future[_] = {
18+
println("=" * 60)
19+
println("Getting file metadata")
20+
println("=" * 60)
21+
println()
22+
23+
service.getFileMetadata(fileId).map {
24+
case Some(metadata) =>
25+
println(s"File metadata retrieved successfully!")
26+
println(s" File ID: ${metadata.id}")
27+
println(s" Filename: ${metadata.filename}")
28+
println(s" MIME type: ${metadata.mimeType}")
29+
println(s" Size: ${metadata.sizeBytes} bytes")
30+
println(s" Created: ${metadata.createdAt}")
31+
println(s" Downloadable: ${metadata.downloadable}")
32+
println()
33+
println("=" * 60)
34+
println("Note: Use this to check if a file exists before downloading")
35+
println("=" * 60)
36+
37+
case None =>
38+
println(s"File not found: $fileId")
39+
println()
40+
println("=" * 60)
41+
println("Tips:")
42+
println(" - The file may have been deleted")
43+
println(" - Check the file ID is correct")
44+
println(" - Use AnthropicListFiles to see available files")
45+
println("=" * 60)
46+
}
47+
}
48+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.cequence.openaiscala.examples.anthropic.files
2+
3+
import io.cequence.openaiscala.anthropic.service.{AnthropicService, AnthropicServiceFactory}
4+
import io.cequence.openaiscala.examples.ExampleBase
5+
6+
import scala.concurrent.Future
7+
8+
// requires `openai-scala-anthropic-client` as a dependency and `ANTHROPIC_API_KEY` environment variable to be set
9+
object AnthropicListFiles extends ExampleBase[AnthropicService] {
10+
11+
override protected val service: AnthropicService = AnthropicServiceFactory()
12+
13+
override protected def run: Future[_] = {
14+
println("=" * 60)
15+
println("Listing files in workspace")
16+
println("=" * 60)
17+
println()
18+
19+
// List first 10 files
20+
service.listFiles(limit = Some(10)).map { response =>
21+
println(s"Files found: ${response.data.length}")
22+
println(s"Has more: ${response.hasMore}")
23+
response.firstId.foreach(id => println(s"First ID: $id"))
24+
response.lastId.foreach(id => println(s"Last ID: $id"))
25+
println()
26+
27+
if (response.data.isEmpty) {
28+
println("No files in workspace")
29+
println()
30+
println("=" * 60)
31+
println("Note: Use createFile to upload files first")
32+
println("=" * 60)
33+
} else {
34+
response.data.foreach { file =>
35+
println(s"File ID: ${file.id}")
36+
println(s" Filename: ${file.filename}")
37+
println(s" MIME type: ${file.mimeType}")
38+
println(s" Size: ${file.sizeBytes} bytes")
39+
println(s" Created: ${file.createdAt}")
40+
println(s" Downloadable: ${file.downloadable}")
41+
println()
42+
}
43+
44+
println("=" * 60)
45+
println("Pagination:")
46+
println(" - Use beforeId parameter to get previous page")
47+
println(" - Use afterId parameter to get next page")
48+
println(" - Limit ranges from 1 to 1000 (default 20)")
49+
println("=" * 60)
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)