SPB Git

Repository Public

retrieval.md 54.3 KB 2,281 lines markdown

1# Retrieval23> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.45The **Retrieval API** allows you to perform [**semantic search**](#semantic-search) over your data, which is a technique that surfaces semantically similar results—even when they match few or no keywords. Retrieval is useful on its own, but is especially powerful when combined with our models to synthesize responses.67![Retrieval depiction](https://cdn.openai.com/API/docs/images/retrieval-depiction.png)89The Retrieval API is powered by [**vector stores**](#vector-stores), which serve as indices for your data. This guide will cover how to perform semantic search, and go into the details of vector stores.1011## Quickstart1213<li className={s.StandaloneLi} data-number={1}>14  **Create vector store** and upload files.15</li>1617Create vector store with files1819```javascript20import OpenAI from "openai";21const client = new OpenAI();2223const vector_store = await client.vectorStores.create({24  // Create vector store25  name: "Support FAQ",26});2728await client.vectorStores.files.uploadAndPoll(29  vector_store.id,30  // Upload file31  fs.createReadStream("customer_policies.txt")32);33```3435```python36from openai import OpenAI3738client = OpenAI()3940vector_store = client.vector_stores.create(        # Create vector store41    name="Support FAQ",42)4344client.vector_stores.files.upload_and_poll(        # Upload file45    vector_store_id=vector_store.id,46    file=open("customer_policies.txt", "rb")47)48```4950```go51package main5253import (54	"context"55	"fmt"56	"os"5758	"github.com/openai/openai-go/v3"59)6061func main() {62	client := openai.NewClient()63	vectorStore, err := client.VectorStores.New(context.Background(), openai.VectorStoreNewParams{Name: openai.String("Support FAQ")})64	if err != nil {65		panic(err)66	}67	file, err := os.Open("customer_policies.txt")68	if err != nil {69		panic(err)70	}71	defer file.Close()72	_, err = client.VectorStores.Files.UploadAndPoll(context.Background(), vectorStore.ID, openai.FileNewParams{73		File:    openai.File(file, "customer_policies.txt", "text/plain"),74		Purpose: openai.FilePurposeAssistants,75	}, 1000)76	if err != nil {77		panic(err)78	}79	fmt.Println(vectorStore.ID)80}81```8283```java84import com.openai.client.OpenAIClient;85import com.openai.client.okhttp.OpenAIOkHttpClient;86import com.openai.models.files.FileCreateParams;87import com.openai.models.files.FilePurpose;88import com.openai.models.vectorstores.VectorStoreCreateParams;89import com.openai.models.vectorstores.files.FileRetrieveParams;90import com.openai.models.vectorstores.files.VectorStoreFile;91import java.nio.file.Path;9293var store =94    client.vectorStores().create(VectorStoreCreateParams.builder().name("Support FAQ").build());95var uploaded =96    client97        .files()98        .create(99            FileCreateParams.builder()100                .file(Path.of(System.getenv("OPENAI_EXAMPLE_FILE_PATH")))101                .purpose(FilePurpose.ASSISTANTS)102                .build());103var file =104    client105        .vectorStores()106        .files()107        .create(108            store.id(),109            com.openai.models.vectorstores.files.FileCreateParams.builder()110                .fileId(uploaded.id())111                .build());112while (file.status().equals(VectorStoreFile.Status.IN_PROGRESS)) {113  Thread.sleep(1000);114  file =115      client116          .vectorStores()117          .files()118          .retrieve(file.id(), FileRetrieveParams.builder().vectorStoreId(store.id()).build());119}120System.out.println(store.id());121```122123```ruby124require "openai"125require "pathname"126127client = OpenAI::Client.new128store = client.vector_stores.create(name: "Support FAQ")129file = client.vector_stores.files.upload_and_poll(130  store.id,131  file: Pathname("customer_policies.txt"),132  timeout: 600133)134raise "File ingestion ended with status: #{file.status}" unless file.status == OpenAI::VectorStores::VectorStoreFile::Status::COMPLETED135136puts(store.id)137```138139140<li className={s.StandaloneLi} data-number={2}>141  **Send search query** to get relevant results.142</li>143144Search query145146```javascript147const userQuery = "What is the return policy?";148149const results = await client.vectorStores.search(vector_store.id, {150  query: userQuery,151});152```153154```python155user_query = "What is the return policy?"156157results = client.vector_stores.search(158    vector_store_id=vector_store.id,159    query=user_query,160)161```162163```go164package main165166import (167	"context"168	"fmt"169170	"github.com/openai/openai-go/v3"171)172173func main() {174	client := openai.NewClient()175	results, err := client.VectorStores.Search(context.Background(), "vs_123", openai.VectorStoreSearchParams{176		Query: openai.VectorStoreSearchParamsQueryUnion{OfString: openai.String("What is the return policy?")},177	})178	if err != nil {179		panic(err)180	}181	fmt.Println(results.Data)182}183```184185```java186import com.openai.client.OpenAIClient;187import com.openai.client.okhttp.OpenAIOkHttpClient;188import com.openai.models.vectorstores.VectorStoreSearchParams;189190String vectorStoreId = "vs_123";191192var results =193    client194        .vectorStores()195        .search(196            vectorStoreId,197            VectorStoreSearchParams.builder().query("What is the return policy?").build());198199System.out.println(results.data());200```201202```ruby203require "openai"204205client = OpenAI::Client.new206results = client.vector_stores.search("vs_123", query: "What is the return policy?")207puts(results.data&.first&.content)208```209210211To learn how to use the results with our models, check out the [synthesizing212  responses](#synthesizing-responses) section.213214## Semantic search215216**Semantic search** is a technique that leverages [vector embeddings](https://developers.openai.com/api/docs/guides/embeddings) to surface semantically relevant results. Importantly, this includes results with few or no shared keywords, which classical search techniques might miss.217218For example, let's look at potential results for `"When did we go to the moon?"`:219220| Text                                              | Keyword Similarity | Semantic Similarity |221| ------------------------------------------------- | ------------------ | ------------------- |222| The first lunar landing occurred in July of 1969. | 0%                 | 65%                 |223| The first man on the moon was Neil Armstrong.     | 27%                | 43%                 |224| When I ate the moon cake, it was delicious.       | 40%                | 28%                 |225226_(Keyword similarity uses [intersection over union](https://en.wikipedia.org/wiki/Jaccard_index); semantic similarity uses [cosine similarity](https://en.wikipedia.org/wiki/Cosine_similarity) with `text-embedding-3-small`.)_227228Notice how the most relevant result contains none of the words in the search query. This flexibility makes semantic search a powerful technique for querying knowledge bases of any size.229230Semantic search is powered by [vector stores](#vector-stores), which we cover in detail later in the guide. This section will focus on the mechanics of semantic search.231232### Performing semantic search233234You can query a vector store using the `search` function and specifying a `query` in natural language. This will return a list of results, each with the relevant chunks, similarity scores, and file of origin.235236Search query237238```javascript239const results = await client.vectorStores.search(vector_store.id, {240  query: "How many woodchucks are allowed per passenger?",241});242```243244```python245results = client.vector_stores.search(246    vector_store_id=vector_store.id,247    query="How many woodchucks are allowed per passenger?",248)249```250251```go252package main253254import (255	"context"256	"fmt"257258	"github.com/openai/openai-go/v3"259)260261func main() {262	client := openai.NewClient()263	results, err := client.VectorStores.Search(context.Background(), "vs_123", openai.VectorStoreSearchParams{264		Query: openai.VectorStoreSearchParamsQueryUnion{OfString: openai.String("How many woodchucks are allowed per passenger?")},265	})266	if err != nil {267		panic(err)268	}269	fmt.Println(results.Data)270}271```272273```java274import com.openai.client.OpenAIClient;275import com.openai.client.okhttp.OpenAIOkHttpClient;276import com.openai.models.vectorstores.VectorStoreSearchParams;277278String vectorStoreId = "vs_123";279280var results =281    client282        .vectorStores()283        .search(284            vectorStoreId,285            VectorStoreSearchParams.builder()286                .query("How many woodchucks are allowed per passenger?")287                .build());288289System.out.println(results.data());290```291292```ruby293require "openai"294295client = OpenAI::Client.new296results = client.vector_stores.search(297  "vs_123",298  query: "How many woodchucks are allowed per passenger?"299)300puts(results.data&.first&.content)301```302303304Results305306```json307{308  "object": "vector_store.search_results.page",309  "search_query": "How many woodchucks are allowed per passenger?",310  "data": [311    {312      "file_id": "file-12345",313      "filename": "woodchuck_policy.txt",314      "score": 0.85,315      "attributes": {316        "region": "North America",317        "author": "Wildlife Department"318      },319      "content": [320        {321          "type": "text",322          "text": "According to the latest regulations, each passenger is allowed to carry up to two woodchucks."323        },324        {325          "type": "text",326          "text": "Ensure that the woodchucks are properly contained during transport."327        }328      ]329    },330    {331      "file_id": "file-67890",332      "filename": "transport_guidelines.txt",333      "score": 0.75,334      "attributes": {335        "region": "North America",336        "author": "Transport Authority"337      },338      "content": [339        {340          "type": "text",341          "text": "Passengers must adhere to the guidelines set forth by the Transport Authority regarding the transport of woodchucks."342        }343      ]344    }345  ],346  "has_more": false,347  "next_page": null348}349```350351352A response will contain 10 results maximum by default, but you can set up to 50 using the `max_num_results` parameter.353354### Query rewriting355356Certain query styles yield better results, so we've provided a setting to automatically rewrite your queries for optimal performance. Enable this feature by setting `rewrite_query=true` when performing a `search`.357358The rewritten query will be available in the result's `search_query` field.359360| **Original**                                                          | **Rewritten**                              |361| --------------------------------------------------------------------- | ------------------------------------------ |362| I'd like to know the height of the main office building.              | primary office building height             |363| What are the safety regulations for transporting hazardous materials? | safety regulations for hazardous materials |364| How do I file a complaint about a service issue?                      | service complaint filing process           |365366### Attribute filtering367368Attribute filtering helps narrow down results by applying criteria, such as restricting searches to a specific date range. You can define and combine criteria in `attribute_filter` to target files based on their attributes before performing semantic search.369370Use **comparison filters** to compare a specific `key` in a file's `attributes` with a given `value`, and **compound filters** to combine multiple filters using `and` and `or`.371372Comparison filter373374```json375{376  "type": "eq" | "ne" | "gt" | "gte" | "lt" | "lte" | "in" | "nin",  // comparison operators377  "key": "attributes_key",                           // attributes key378  "value": "target_value"                             // value to compare against379}380```381382383Compound filter384385```json386{387  "type": "and" | "or",                                // logical operators388  "filters": [...]389}390```391392393Below are some example filters.394395396397Region398399    Filter for a region400401```json402{403  "type": "eq",404  "key": "region",405  "value": "us"406}407```408409  410411  412413    414Date range415416    Filter for a date range417418```json419{420  "type": "and",421  "filters": [422    {423      "type": "gte",424      "key": "date",425      "value": 1704067200  // unix timestamp for 2024-01-01426    },427    {428      "type": "lte",429      "key": "date",430      "value": 1710892800  // unix timestamp for 2024-03-20431    }432  ]433}434```435436  437438  439440    441Filenames442443    Filter to match any of a set of filenames444445```json446{447  "type": "in",448  "property": "filename",449  "value": ["example.txt", "example2.txt"]450}451```452453  454455  456457    458Exclude filenames459460    Filter to exclude drafts by filename461462```json463{464  "type": "nin",465  "property": "filename",466  "value": ["draft.txt", "internal_notes.md"]467}468```469470  471472  473474    475Complex476477    Filter for top secret projects with certain names in english478479```json480{481  "type": "or",482  "filters": [483    {484      "type": "and",485      "filters": [486        {487          "type": "or",488          "filters": [489            {490              "type": "eq",491              "key": "project_code",492              "value": "X123"493            },494            {495              "type": "eq",496              "key": "project_code",497              "value": "X999"498            }499          ]500        },501        {502          "type": "eq",503          "key": "confidentiality",504          "value": "top_secret"505        }506      ]507    },508    {509      "type": "eq",510      "key": "language",511      "value": "en"512    }513  ]514}515```516517518519### Ranking520521If you find that your file search results are not sufficiently relevant, you can adjust the `ranking_options` to improve the quality of responses. This includes specifying a `ranker`, such as `auto` or `default-2024-08-21`, and setting a `score_threshold` between 0.0 and 1.0. A higher `score_threshold` will limit the results to more relevant chunks, though it may exclude some potentially useful ones. When `ranking_options.hybrid_search` is provided you can also tune `hybrid_search.embedding_weight` (`rrf_embedding_weight`) and `hybrid_search.text_weight` (`rrf_text_weight`) to control how reciprocal rank fusion balances semantic embedding matches vs. sparse keyword matches. Increase the former to emphasize semantic similarity, increase the latter to emphasize textual overlap, and ensure at least one of the weights is greater than zero.522523## Vector stores524525Vector stores are the containers that power semantic search for the Retrieval API and the [file search](https://developers.openai.com/api/docs/guides/tools-file-search) tool. When you add a file to a vector store it will be automatically chunked, embedded, and indexed.526527Vector stores contain `vector_store_file` objects, which are backed by a `file` object.528529| Object type | Description                                                                                                                                                                           |530| -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |531| `file`                                                                     | Represents content uploaded through the [Files API](https://developers.openai.com/api/reference/resources/files). Often used with vector stores, but also for fine-tuning and other use cases.                      |532| `vector_store`                                                             | Container for searchable files.                                                                                                                                                       |533| `vector_store.file`                                                        | Wrapper type specifically representing a `file` that has been chunked and embedded, and has been associated with a `vector_store`. <br />Contains `attributes` map used for filtering. |534535### Pricing536537You will be charged based on the total storage used across all your vector stores, determined by the size of parsed chunks and their corresponding embeddings.538539| Storage                        | Cost         |540| ------------------------------ | ------------ |541| Up to 1 GB (across all stores) | Free         |542| Beyond 1 GB                    | $0.10/GB/day |543544See [expiration policies](#expiration-policies) for options to minimize costs.545546### Vector store operations547548549550Create551552    Create vector store553554```javascript555await client.vectorStores.create({556  name: "Support FAQ",557  file_ids: ["file_123"],558});559```560561```python562client.vector_stores.create(563    name="Support FAQ",564    file_ids=["file_123"]565)566```567568```go569package main570571import (572	"context"573	"fmt"574575	"github.com/openai/openai-go/v3"576)577578func main() {579	client := openai.NewClient()580	vectorStore, err := client.VectorStores.New(context.Background(), openai.VectorStoreNewParams{581		Name:    openai.String("Support FAQ"),582		FileIDs: []string{"file_123"},583	})584	if err != nil {585		panic(err)586	}587	fmt.Println(vectorStore.ID)588}589```590591```java592import com.openai.client.OpenAIClient;593import com.openai.client.okhttp.OpenAIOkHttpClient;594import com.openai.models.vectorstores.VectorStoreCreateParams;595596String fileId = "file_123";597598var store =599    client600        .vectorStores()601        .create(602            VectorStoreCreateParams.builder().name("Support FAQ").addFileId(fileId).build());603604System.out.println(store.id());605```606607```ruby608require "openai"609610client = OpenAI::Client.new611store = client.vector_stores.create(612  name: "Support FAQ",613  file_ids: ["file_123"]614)615puts(store.id)616```617618  619620  621622    623Retrieve624625    Retrieve vector store626627```javascript628await client.vectorStores.retrieve("vs_123");629```630631```python632client.vector_stores.retrieve(633    vector_store_id="vs_123"634)635```636637```go638package main639640import (641	"context"642	"fmt"643644	"github.com/openai/openai-go/v3"645)646647func main() {648	client := openai.NewClient()649	vectorStore, err := client.VectorStores.Get(context.Background(), "vs_123")650	if err != nil {651		panic(err)652	}653	fmt.Println(vectorStore.ID)654}655```656657```java658import com.openai.client.OpenAIClient;659import com.openai.client.okhttp.OpenAIOkHttpClient;660661String vectorStoreId = "vs_123";662663System.out.println(client.vectorStores().retrieve(vectorStoreId).id());664```665666```ruby667require "openai"668669client = OpenAI::Client.new670store = client.vector_stores.retrieve("vs_123")671puts(store.id)672```673674  675676  677678    679Update680681    Update vector store682683```javascript684await client.vectorStores.update("vs_123", {685  name: "Support FAQ Updated",686});687```688689```python690client.vector_stores.update(691    vector_store_id="vs_123",692    name="Support FAQ Updated"693)694```695696```go697package main698699import (700	"context"701	"fmt"702703	"github.com/openai/openai-go/v3"704)705706func main() {707	client := openai.NewClient()708	vectorStore, err := client.VectorStores.Update(context.Background(), "vs_123", openai.VectorStoreUpdateParams{709		Name: openai.String("Support FAQ Updated"),710	})711	if err != nil {712		panic(err)713	}714	fmt.Println(vectorStore.Name)715}716```717718```java719import com.openai.client.OpenAIClient;720import com.openai.client.okhttp.OpenAIOkHttpClient;721import com.openai.models.vectorstores.VectorStoreUpdateParams;722723String vectorStoreId = "vs_123";724725var store =726    client727        .vectorStores()728        .update(729            vectorStoreId,730            VectorStoreUpdateParams.builder().name("Updated knowledge base").build());731732System.out.println(store.name());733```734735```ruby736require "openai"737738client = OpenAI::Client.new739store = client.vector_stores.update("vs_123", name: "Updated knowledge base")740puts(store.name)741```742743  744745  746747    748Delete749750    Delete vector store751752```javascript753await client.vectorStores.delete("vs_123");754```755756```python757client.vector_stores.delete(758    vector_store_id="vs_123"759)760```761762```go763package main764765import (766	"context"767	"fmt"768769	"github.com/openai/openai-go/v3"770)771772func main() {773	client := openai.NewClient()774	deleted, err := client.VectorStores.Delete(context.Background(), "vs_123")775	if err != nil {776		panic(err)777	}778	fmt.Println(deleted.Deleted)779}780```781782```java783import com.openai.client.OpenAIClient;784import com.openai.client.okhttp.OpenAIOkHttpClient;785786String vectorStoreId = "vs_123";787788System.out.println(client.vectorStores().delete(vectorStoreId).deleted());789```790791```ruby792require "openai"793794client = OpenAI::Client.new795deleted = client.vector_stores.delete("vs_123")796puts(deleted.deleted)797```798799  800801  802803    804List805806    List vector stores807808```javascript809await client.vectorStores.list();810```811812```python813client.vector_stores.list()814```815816```go817package main818819import (820	"context"821	"fmt"822823	"github.com/openai/openai-go/v3"824)825826func main() {827	client := openai.NewClient()828	vectorStores, err := client.VectorStores.List(context.Background(), openai.VectorStoreListParams{})829	if err != nil {830		panic(err)831	}832	fmt.Println(vectorStores.Data)833}834```835836```java837import com.openai.client.OpenAIClient;838import com.openai.client.okhttp.OpenAIOkHttpClient;839840System.out.println(client.vectorStores().list().data());841```842843```ruby844require "openai"845846client = OpenAI::Client.new847stores = client.vector_stores.list(limit: 10)848puts((stores.data || []).length)849```850851852853### Vector store file operations854855Some operations, like `create` for `vector_store.file`, are asynchronous and may take time to complete—use our helper functions, like `create_and_poll` to block until it is. Otherwise, you may check the status. Removing files from a vector store is eventually consistent, and search results may still include content from a removed file for a short period.856857Adding files is rate limited per vector store ID. Requests to [`/vector_stores/{vector_store_id}/files`](https://developers.openai.com/api/reference/resources/vector_stores/subresources/files/methods/create) and [`/vector_stores/{vector_store_id}/file_batches`](https://developers.openai.com/api/reference/resources/vector_stores/subresources/file_batches/methods/create) share a per-vector-store limit of 300 requests per minute.858859860861Create862863    Create vector store file864865```javascript866await client.vectorStores.files.createAndPoll("vs_123", {867  file_id: "file_123",868});869```870871```python872client.vector_stores.files.create_and_poll(873    vector_store_id="vs_123",874    file_id="file_123"875)876```877878```go879package main880881import (882	"context"883	"fmt"884885	"github.com/openai/openai-go/v3"886)887888func main() {889	client := openai.NewClient()890	file, err := client.VectorStores.Files.NewAndPoll(context.Background(), "vs_123", openai.VectorStoreFileNewParams{891		FileID: "file_123",892	}, 1000)893	if err != nil {894		panic(err)895	}896	fmt.Println(file.ID)897}898```899900```java901import com.openai.client.OpenAIClient;902import com.openai.client.okhttp.OpenAIOkHttpClient;903import com.openai.models.vectorstores.files.FileCreateParams;904905String vectorStoreId = "vs_123";906907String fileId = "file_123";908909var file =910    client911        .vectorStores()912        .files()913        .create(vectorStoreId, FileCreateParams.builder().fileId(fileId).build());914915System.out.println(file.id());916```917918```ruby919require "openai"920921client = OpenAI::Client.new922file = client.vector_stores.files.create("vs_123", file_id: "file_123")923puts(file.id)924```925926  927928  929930    931Upload932933    Upload vector store file934935```javascript936await client.vectorStores.files.uploadAndPoll(937  "vs_123",938  fs.createReadStream("customer_policies.txt")939);940```941942```python943client.vector_stores.files.upload_and_poll(944    vector_store_id="vs_123",945    file=open("customer_policies.txt", "rb")946)947```948949```go950package main951952import (953	"context"954	"fmt"955	"os"956957	"github.com/openai/openai-go/v3"958)959960func main() {961	client := openai.NewClient()962	file, err := os.Open("customer_policies.txt")963	if err != nil {964		panic(err)965	}966	defer file.Close()967	result, err := client.VectorStores.Files.UploadAndPoll(context.Background(), "vs_123", openai.FileNewParams{968		File:    openai.File(file, "customer_policies.txt", "text/plain"),969		Purpose: openai.FilePurposeAssistants,970	}, 1000)971	if err != nil {972		panic(err)973	}974	fmt.Println(result.ID)975}976```977978```java979import com.openai.client.OpenAIClient;980import com.openai.client.okhttp.OpenAIOkHttpClient;981import com.openai.models.files.FileCreateParams;982import com.openai.models.files.FilePurpose;983import com.openai.models.vectorstores.files.FileRetrieveParams;984import com.openai.models.vectorstores.files.VectorStoreFile;985import java.nio.file.Path;986987String vectorStoreId = "vs_123";988var uploaded =989    client990        .files()991        .create(992            FileCreateParams.builder()993                .file(Path.of(System.getenv("OPENAI_EXAMPLE_FILE_PATH")))994                .purpose(FilePurpose.ASSISTANTS)995                .build());996var file =997    client998        .vectorStores()999        .files()1000        .create(1001            vectorStoreId,1002            com.openai.models.vectorstores.files.FileCreateParams.builder()1003                .fileId(uploaded.id())1004                .build());1005while (file.status().equals(VectorStoreFile.Status.IN_PROGRESS)) {1006  Thread.sleep(1000);1007  file =1008      client1009          .vectorStores()1010          .files()1011          .retrieve(1012              file.id(), FileRetrieveParams.builder().vectorStoreId(vectorStoreId).build());1013}1014System.out.println(file.id());1015```10161017```ruby1018require "openai"1019require "pathname"10201021client = OpenAI::Client.new1022vector_store_file = client.vector_stores.files.upload_and_poll(1023  "vs_123",1024  file: Pathname("customer_policies.txt"),1025  timeout: 6001026)1027raise "File ingestion ended with status: #{vector_store_file.status}" unless vector_store_file.status == OpenAI::VectorStores::VectorStoreFile::Status::COMPLETED10281029puts(vector_store_file.id)1030```10311032  10331034  10351036    1037Retrieve10381039    Retrieve vector store file10401041```javascript1042await client.vectorStores.files.retrieve("file_123", {1043  vector_store_id: "vs_123",1044});1045```10461047```python1048client.vector_stores.files.retrieve(1049    vector_store_id="vs_123",1050    file_id="file_123"1051)1052```10531054```go1055package main10561057import (1058	"context"1059	"fmt"10601061	"github.com/openai/openai-go/v3"1062)10631064func main() {1065	client := openai.NewClient()1066	file, err := client.VectorStores.Files.Get(context.Background(), "vs_123", "file_123")1067	if err != nil {1068		panic(err)1069	}1070	fmt.Println(file.ID)1071}1072```10731074```java1075import com.openai.client.OpenAIClient;1076import com.openai.client.okhttp.OpenAIOkHttpClient;10771078String fileId = "file_123";10791080String vectorStoreId = "vs_123";10811082System.out.println(1083    client1084        .vectorStores()1085        .files()1086        .retrieve(1087            fileId,1088            com.openai.models.vectorstores.files.FileRetrieveParams.builder()1089                .vectorStoreId(vectorStoreId)1090                .build())1091        .id());1092```10931094```ruby1095require "openai"10961097client = OpenAI::Client.new1098file = client.vector_stores.files.retrieve("file_123", vector_store_id: "vs_123")1099puts(file.id)1100```11011102  11031104  11051106    1107Update11081109    Update vector store file11101111```javascript1112await client.vectorStores.files.update("file_123", {1113  vector_store_id: "vs_123",1114  attributes: { key: "value" },1115});1116```11171118```python1119client.vector_stores.files.update(1120    vector_store_id="vs_123",1121    file_id="file_123",1122    attributes={"key": "value"}1123)1124```11251126```go1127package main11281129import (1130	"context"1131	"fmt"11321133	"github.com/openai/openai-go/v3"1134)11351136func main() {1137	client := openai.NewClient()1138	file, err := client.VectorStores.Files.Update(context.Background(), "vs_123", "file_123", openai.VectorStoreFileUpdateParams{1139		Attributes: map[string]openai.VectorStoreFileUpdateParamsAttributeUnion{1140			"key": {OfString: openai.String("value")},1141		},1142	})1143	if err != nil {1144		panic(err)1145	}1146	fmt.Println(file.ID)1147}1148```11491150```java1151import com.openai.client.OpenAIClient;1152import com.openai.client.okhttp.OpenAIOkHttpClient;1153import com.openai.core.JsonValue;1154import com.openai.models.vectorstores.files.FileUpdateParams;11551156String fileId = "file_123";11571158String vectorStoreId = "vs_123";11591160var file =1161    client1162        .vectorStores()1163        .files()1164        .update(1165            fileId,1166            FileUpdateParams.builder()1167                .vectorStoreId(vectorStoreId)1168                .attributes(1169                    FileUpdateParams.Attributes.builder()1170                        .putAdditionalProperty("category", JsonValue.from("policy"))1171                        .build())1172                .build());11731174System.out.println(file.id());1175```11761177```ruby1178require "openai"11791180client = OpenAI::Client.new1181file = client.vector_stores.files.update("file_123", vector_store_id: "vs_123", attributes: { category: "policy" })1182puts(file.id)1183```11841185  11861187  11881189    1190Delete11911192    Delete vector store file11931194```javascript1195await client.vectorStores.files.delete("file_123", {1196  vector_store_id: "vs_123",1197});1198```11991200```python1201client.vector_stores.files.delete(1202    vector_store_id="vs_123",1203    file_id="file_123"1204)1205```12061207```go1208package main12091210import (1211	"context"1212	"fmt"12131214	"github.com/openai/openai-go/v3"1215)12161217func main() {1218	client := openai.NewClient()1219	deleted, err := client.VectorStores.Files.Delete(context.Background(), "vs_123", "file_123")1220	if err != nil {1221		panic(err)1222	}1223	fmt.Println(deleted.Deleted)1224}1225```12261227```java1228import com.openai.client.OpenAIClient;1229import com.openai.client.okhttp.OpenAIOkHttpClient;12301231String fileId = "file_123";12321233String vectorStoreId = "vs_123";12341235System.out.println(1236    client1237        .vectorStores()1238        .files()1239        .delete(1240            fileId,1241            com.openai.models.vectorstores.files.FileDeleteParams.builder()1242                .vectorStoreId(vectorStoreId)1243                .build())1244        .deleted());1245```12461247```ruby1248require "openai"12491250client = OpenAI::Client.new1251deleted = client.vector_stores.files.delete("file_123", vector_store_id: "vs_123")1252puts(deleted.deleted)1253```12541255  12561257  12581259    1260List12611262    List vector store files12631264```javascript1265await client.vectorStores.files.list("vs_123");1266```12671268```python1269client.vector_stores.files.list(1270    vector_store_id="vs_123"1271)1272```12731274```go1275package main12761277import (1278	"context"1279	"fmt"12801281	"github.com/openai/openai-go/v3"1282)12831284func main() {1285	client := openai.NewClient()1286	files, err := client.VectorStores.Files.List(context.Background(), "vs_123", openai.VectorStoreFileListParams{})1287	if err != nil {1288		panic(err)1289	}1290	fmt.Println(files.Data)1291}1292```12931294```java1295import com.openai.client.OpenAIClient;1296import com.openai.client.okhttp.OpenAIOkHttpClient;12971298String vectorStoreId = "vs_123";12991300System.out.println(client.vectorStores().files().list(vectorStoreId).data());1301```13021303```ruby1304require "openai"13051306client = OpenAI::Client.new1307files = client.vector_stores.files.list("vs_123")1308puts((files.data || []).length)1309```1310131113121313### Batch operations1314131513161317Create13181319    Batch create operation13201321```javascript1322await client.vectorStores.fileBatches.createAndPoll("vs_123", {1323  files: [1324    {1325      file_id: "file_123",1326      attributes: { department: "finance" },1327    },1328    {1329      file_id: "file_456",1330      chunking_strategy: {1331        type: "static",1332        static: {1333          max_chunk_size_tokens: 1200,1334          chunk_overlap_tokens: 200,1335        },1336      },1337    },1338  ],1339});1340```13411342```python1343client.vector_stores.file_batches.create_and_poll(1344    vector_store_id="vs_123",1345    files=[1346        {1347            "file_id": "file_123",1348            "attributes": {"department": "finance"}1349        },1350        {1351            "file_id": "file_456",1352            "chunking_strategy": {1353                "type": "static",1354                "max_chunk_size_tokens": 1200,1355                "chunk_overlap_tokens": 2001356            }1357        }1358    ]1359)1360```13611362```go1363package main13641365import (1366	"context"1367	"fmt"13681369	"github.com/openai/openai-go/v3"1370)13711372func main() {1373	client := openai.NewClient()1374	batch, err := client.VectorStores.FileBatches.NewAndPoll(context.Background(), "vs_123", openai.VectorStoreFileBatchNewParams{1375		Files: []openai.VectorStoreFileBatchNewParamsFile{1376			{1377				FileID: "file_123",1378				Attributes: map[string]openai.VectorStoreFileBatchNewParamsFileAttributeUnion{1379					"department": {OfString: openai.String("finance")},1380				},1381			},1382			{1383				FileID: "file_456",1384				ChunkingStrategy: openai.FileChunkingStrategyParamUnion{OfStatic: &openai.StaticFileChunkingStrategyObjectParam{1385					Static: openai.StaticFileChunkingStrategyParam{MaxChunkSizeTokens: 1200, ChunkOverlapTokens: 200},1386				}},1387			},1388		},1389	}, 1000)1390	if err != nil {1391		panic(err)1392	}1393	fmt.Println(batch.ID)1394}1395```13961397```java1398import com.openai.client.OpenAIClient;1399import com.openai.client.okhttp.OpenAIOkHttpClient;1400import com.openai.core.JsonValue;1401import com.openai.models.vectorstores.StaticFileChunkingStrategy;1402import com.openai.models.vectorstores.filebatches.FileBatchCreateParams;1403import com.openai.models.vectorstores.filebatches.FileBatchRetrieveParams;1404import com.openai.models.vectorstores.filebatches.VectorStoreFileBatch;14051406String vectorStoreId = "vs_123";1407String fileId = "file_123";1408String fileId2 = "file_456";1409var first =1410    FileBatchCreateParams.File.builder()1411        .fileId(fileId)1412        .attributes(1413            FileBatchCreateParams.File.Attributes.builder()1414                .putAdditionalProperty("department", JsonValue.from("finance"))1415                .build())1416        .build();1417var second =1418    FileBatchCreateParams.File.builder()1419        .fileId(fileId2)1420        .staticChunkingStrategy(1421            StaticFileChunkingStrategy.builder()1422                .maxChunkSizeTokens(1200)1423                .chunkOverlapTokens(200)1424                .build())1425        .build();14261427var batch =1428    client1429        .vectorStores()1430        .fileBatches()1431        .create(1432            vectorStoreId,1433            FileBatchCreateParams.builder().addFile(first).addFile(second).build());1434while (batch.status().equals(VectorStoreFileBatch.Status.IN_PROGRESS)) {1435  Thread.sleep(1000);1436  batch =1437      client1438          .vectorStores()1439          .fileBatches()1440          .retrieve(1441              batch.id(),1442              FileBatchRetrieveParams.builder().vectorStoreId(vectorStoreId).build());1443}1444System.out.println(batch.status());1445```14461447```ruby1448require "openai"14491450client = OpenAI::Client.new1451batch = client.vector_stores.file_batches.create_and_poll(1452  "vs_123",1453  files: [1454    {1455      file_id: "file_123",1456      attributes: { department: "finance" }1457    },1458    {1459      file_id: "file_456",1460      chunking_strategy: {1461        type: :static,1462        static: {1463          max_chunk_size_tokens: 1_200,1464          chunk_overlap_tokens: 2001465        }1466      }1467    }1468  ],1469  timeout: 6001470)1471raise "File ingestion ended with status: #{batch.status}" unless batch.status == OpenAI::VectorStores::VectorStoreFileBatch::Status::COMPLETED14721473raise "File ingestion failed for #{batch.file_counts.failed} file(s)" if batch.file_counts.failed.positive?14741475# Live validation of per-file batches returned default chunking despite overrides.1476file = client.vector_stores.files.retrieve("file_456", vector_store_id: "vs_123")1477strategy = file.chunking_strategy1478unless strategy.is_a?(OpenAI::StaticFileChunkingStrategyObject) &&1479       strategy.static.max_chunk_size_tokens == 1_200 &&1480       strategy.static.chunk_overlap_tokens == 2001481  raise "Requested chunking was not applied to #{file.id}: #{strategy.to_json}"1482end14831484puts(batch.status)1485```14861487  14881489  14901491    1492Retrieve14931494    Batch retrieve operation14951496```javascript1497await client.vectorStores.fileBatches.retrieve("vsfb_123", {1498  vector_store_id: "vs_123",1499});1500```15011502```python1503client.vector_stores.file_batches.retrieve(1504    vector_store_id="vs_123",1505    batch_id="vsfb_123"1506)1507```15081509```go1510package main15111512import (1513	"context"1514	"fmt"15151516	"github.com/openai/openai-go/v3"1517)15181519func main() {1520	client := openai.NewClient()1521	batch, err := client.VectorStores.FileBatches.Get(context.Background(), "vs_123", "vsfb_123")1522	if err != nil {1523		panic(err)1524	}1525	fmt.Println(batch.ID)1526}1527```15281529```java1530import com.openai.client.OpenAIClient;1531import com.openai.client.okhttp.OpenAIOkHttpClient;15321533String fileBatchId = "vsfb_123";15341535String vectorStoreId = "vs_123";15361537System.out.println(1538    client1539        .vectorStores()1540        .fileBatches()1541        .retrieve(1542            fileBatchId,1543            com.openai.models.vectorstores.filebatches.FileBatchRetrieveParams.builder()1544                .vectorStoreId(vectorStoreId)1545                .build())1546        .status());1547```15481549```ruby1550require "openai"15511552client = OpenAI::Client.new1553batch = client.vector_stores.file_batches.retrieve(1554  "vsfb_123",1555  vector_store_id: "vs_123"1556)1557puts(batch.status)1558```15591560  15611562  15631564    1565Cancel15661567    Batch cancel operation15681569```javascript1570await client.vectorStores.fileBatches.cancel("vsfb_123", {1571  vector_store_id: "vs_123",1572});1573```15741575```python1576client.vector_stores.file_batches.cancel(1577    vector_store_id="vs_123",1578    batch_id="vsfb_123"1579)1580```15811582```go1583package main15841585import (1586	"context"1587	"fmt"15881589	"github.com/openai/openai-go/v3"1590)15911592func main() {1593	client := openai.NewClient()1594	batch, err := client.VectorStores.FileBatches.Cancel(context.Background(), "vs_123", "vsfb_123")1595	if err != nil {1596		panic(err)1597	}1598	fmt.Println(batch.Status)1599}1600```16011602```java1603import com.openai.client.OpenAIClient;1604import com.openai.client.okhttp.OpenAIOkHttpClient;16051606String fileBatchId = "vsfb_123";16071608String vectorStoreId = "vs_123";16091610System.out.println(1611    client1612        .vectorStores()1613        .fileBatches()1614        .cancel(1615            fileBatchId,1616            com.openai.models.vectorstores.filebatches.FileBatchCancelParams.builder()1617                .vectorStoreId(vectorStoreId)1618                .build())1619        .status());1620```16211622```ruby1623require "openai"16241625client = OpenAI::Client.new1626batch = client.vector_stores.file_batches.cancel(1627  "vsfb_123",1628  vector_store_id: "vs_123"1629)1630puts(batch.status)1631```16321633  16341635  16361637    1638List16391640    List files in a batch16411642```javascript1643await client.vectorStores.fileBatches.listFiles("vsfb_123", {1644  vector_store_id: "vs_123",1645});1646```16471648```python1649client.vector_stores.file_batches.list_files(1650    "vsfb_123",1651    vector_store_id="vs_123"1652)1653```16541655```go1656package main16571658import (1659	"context"1660	"fmt"16611662	"github.com/openai/openai-go/v3"1663)16641665func main() {1666	client := openai.NewClient()1667	files, err := client.VectorStores.FileBatches.ListFiles(context.Background(), "vs_123", "vsfb_123", openai.VectorStoreFileBatchListFilesParams{})1668	if err != nil {1669		panic(err)1670	}1671	fmt.Println(files.Data)1672}1673```16741675```java1676import com.openai.client.OpenAIClient;1677import com.openai.client.okhttp.OpenAIOkHttpClient;16781679String fileBatchId = "vsfb_123";16801681String vectorStoreId = "vs_123";16821683System.out.println(1684    client1685        .vectorStores()1686        .fileBatches()1687        .listFiles(1688            fileBatchId,1689            com.openai.models.vectorstores.filebatches.FileBatchListFilesParams.builder()1690                .vectorStoreId(vectorStoreId)1691                .build())1692        .data());1693```16941695```ruby1696require "openai"16971698client = OpenAI::Client.new1699files = client.vector_stores.file_batches.list_files(1700  "vsfb_123",1701  vector_store_id: "vs_123"1702)1703puts((files.data || []).length)1704```1705170617071708When creating a batch you can either provide `file_ids` with optional `attributes` and/or `chunking_strategy`, or use the `files` array to pass objects that include a `file_id` plus optional `attributes` and `chunking_strategy` for each file. The two options are mutually exclusive so that you can cleanly control whether every file shares the same settings or you need per-file overrides.17091710For higher-throughput ingestion into a single vector store, we recommend batch creation whenever possible. Batches can include up to 500 files in one request, which usually reduces contention and improves end-to-end latency versus sending many single-file create requests.17111712### Attributes17131714Each `vector_store.file` can have associated `attributes`, a dictionary of values that can be referenced when performing [semantic search](#semantic-search) with [attribute filtering](#attribute-filtering). The dictionary can have at most 16 keys, with a limit of 256 characters each.17151716Create vector store file with attributes17171718```javascript1719await client.vectorStores.files.create("<vector_store_id>", {1720  file_id: "file_123",1721  attributes: {1722    region: "US",1723    category: "Marketing",1724    date: 1672531200, // Jan 1, 20231725  },1726});1727```17281729```python1730client.vector_stores.files.create(1731    vector_store_id="<vector_store_id>",1732    file_id="file_123",1733    attributes={1734        "region": "US",1735        "category": "Marketing",1736        "date": 1672531200      # Jan 1, 20231737    }1738)1739```17401741```go1742package main17431744import (1745	"context"1746	"fmt"17471748	"github.com/openai/openai-go/v3"1749)17501751func main() {1752	client := openai.NewClient()1753	file, err := client.VectorStores.Files.New(context.Background(), "<vector_store_id>", openai.VectorStoreFileNewParams{1754		FileID: "file_123",1755		Attributes: map[string]openai.VectorStoreFileNewParamsAttributeUnion{1756			"region":   {OfString: openai.String("US")},1757			"category": {OfString: openai.String("Marketing")},1758			"date":     {OfFloat: openai.Float(1672531200)},1759		},1760	})1761	if err != nil {1762		panic(err)1763	}1764	fmt.Println(file.ID)1765}1766```17671768```java1769import com.openai.client.OpenAIClient;1770import com.openai.client.okhttp.OpenAIOkHttpClient;1771import com.openai.core.JsonValue;1772import com.openai.models.vectorstores.files.FileCreateParams;17731774String vectorStoreId = "<vector_store_id>";17751776String fileId = "file_123";17771778var file =1779    client1780        .vectorStores()1781        .files()1782        .create(1783            vectorStoreId,1784            FileCreateParams.builder()1785                .fileId(fileId)1786                .attributes(1787                    FileCreateParams.Attributes.builder()1788                        .putAdditionalProperty("category", JsonValue.from("policy"))1789                        .build())1790                .build());17911792System.out.println(file.id());1793```17941795```ruby1796require "openai"17971798client = OpenAI::Client.new1799file = client.vector_stores.files.create("<vector_store_id>", file_id: "file_123", attributes: { category: "policy" })1800puts(file.id)1801```180218031804### Expiration policies18051806You can set an expiration policy on `vector_store` objects with `expires_after`. Once a vector store expires, all associated `vector_store.file` objects will be deleted and you'll no longer be charged for them.18071808Set expiration policy for vector store18091810```javascript1811await client.vectorStores.update("vs_123", {1812  expires_after: {1813    anchor: "last_active_at",1814    days: 7,1815  },1816});1817```18181819```python1820client.vector_stores.update(1821    vector_store_id="vs_123",1822    expires_after={1823        "anchor": "last_active_at",1824        "days": 71825    }1826)1827```18281829```go1830package main18311832import (1833	"context"1834	"fmt"18351836	"github.com/openai/openai-go/v3"1837)18381839func main() {1840	client := openai.NewClient()1841	vectorStore, err := client.VectorStores.Update(context.Background(), "vs_123", openai.VectorStoreUpdateParams{1842		ExpiresAfter: openai.VectorStoreUpdateParamsExpiresAfter{Days: 7},1843	})1844	if err != nil {1845		panic(err)1846	}1847	fmt.Println(vectorStore.ExpiresAfter)1848}1849```18501851```java1852import com.openai.client.OpenAIClient;1853import com.openai.client.okhttp.OpenAIOkHttpClient;1854import com.openai.core.JsonValue;1855import com.openai.models.vectorstores.VectorStoreUpdateParams;18561857String vectorStoreId = "vs_123";18581859var store =1860    client1861        .vectorStores()1862        .update(1863            vectorStoreId,1864            VectorStoreUpdateParams.builder()1865                .expiresAfter(1866                    VectorStoreUpdateParams.ExpiresAfter.builder()1867                        .anchor(JsonValue.from("last_active_at"))1868                        .days(7)1869                        .build())1870                .build());18711872System.out.println(store.expiresAfter().orElseThrow());1873```18741875```ruby1876require "openai"18771878client = OpenAI::Client.new1879store = client.vector_stores.update(1880  "vs_123",1881  expires_after: {1882    anchor: :last_active_at,1883    days: 71884  }1885)1886puts(store.expires_after)1887```188818891890### Limits18911892The maximum file size is 512 MB. Each file should contain no more than 5,000,000 tokens per file (computed automatically when you attach a file).18931894### Chunking18951896By default, `max_chunk_size_tokens` is set to `800` and `chunk_overlap_tokens` is set to `400`, meaning every file is indexed by being split up into 800-token chunks, with 400-token overlap between consecutive chunks.18971898You can adjust this by setting [`chunking_strategy`](https://developers.openai.com/api/reference/resources/vector_stores/subresources/files/methods/create#vector-stores-files-createfile-chunking_strategy) when adding files to the vector store. The strategy has certain limitations:18991900- `max_chunk_size_tokens` must be between 100 and 4096 inclusive.1901- `chunk_overlap_tokens` must be non-negative and should not exceed `max_chunk_size_tokens / 2`.1902190319041905#### Supported file types1906190719081909_For `text/` MIME types, the encoding must be one of `utf-8`, `utf-16`, or `ascii`._19101911| File format | MIME type                                                                   |1912| ----------- | --------------------------------------------------------------------------- |1913| `.c`        | `text/x-c`                                                                  |1914| `.cpp`      | `text/x-c++`                                                                |1915| `.cs`       | `text/x-csharp`                                                             |1916| `.css`      | `text/css`                                                                  |1917| `.doc`      | `application/msword`                                                        |1918| `.docx`     | `application/vnd.openxmlformats-officedocument.wordprocessingml.document`   |1919| `.go`       | `text/x-golang`                                                             |1920| `.html`     | `text/html`                                                                 |1921| `.java`     | `text/x-java`                                                               |1922| `.js`       | `text/javascript`                                                           |1923| `.json`     | `application/json`                                                          |1924| `.md`       | `text/markdown`                                                             |1925| `.pdf`      | `application/pdf`                                                           |1926| `.php`      | `text/x-php`                                                                |1927| `.pptx`     | `application/vnd.openxmlformats-officedocument.presentationml.presentation` |1928| `.py`       | `text/x-python`                                                             |1929| `.py`       | `text/x-script.python`                                                      |1930| `.rb`       | `text/x-ruby`                                                               |1931| `.sh`       | `application/x-sh`                                                          |1932| `.tex`      | `text/x-tex`                                                                |1933| `.ts`       | `application/typescript`                                                    |1934| `.txt`      | `text/plain`                                                                |193519361937193819391940## Synthesizing responses19411942After performing a query you may want to synthesize a response based on the results. You can leverage our models to do so, by supplying the results and original query, to get back a grounded response.19431944Perform search query to get results19451946```javascript1947import OpenAI from "openai";19481949const client = new OpenAI();19501951const userQuery = "What is the return policy?";19521953const results = await client.vectorStores.search(vector_store.id, {1954  query: userQuery,1955});1956```19571958```python1959from openai import OpenAI19601961client = OpenAI()19621963user_query = "What is the return policy?"19641965results = client.vector_stores.search(1966    vector_store_id=vector_store.id,1967    query=user_query,1968)1969```19701971```go1972package main19731974import (1975	"context"1976	"fmt"19771978	"github.com/openai/openai-go/v3"1979)19801981func main() {1982	client := openai.NewClient()1983	results, err := client.VectorStores.Search(context.Background(), "vs_123", openai.VectorStoreSearchParams{1984		Query: openai.VectorStoreSearchParamsQueryUnion{OfString: openai.String("What is the return policy?")},1985	})1986	if err != nil {1987		panic(err)1988	}1989	fmt.Println(results.Data)1990}1991```19921993```java1994import com.openai.client.OpenAIClient;1995import com.openai.client.okhttp.OpenAIOkHttpClient;1996import com.openai.models.vectorstores.VectorStoreSearchParams;19971998String vectorStoreId = "vs_123";19992000var results =2001    client2002        .vectorStores()2003        .search(2004            vectorStoreId,2005            VectorStoreSearchParams.builder().query("What is the return policy?").build());20062007System.out.println(results.data());2008```20092010```ruby2011require "openai"20122013client = OpenAI::Client.new2014results = client.vector_stores.search(2015  "vs_123",2016  query: "What is the return policy?"2017)2018puts(results.data)2019```202020212022Synthesize a response based on results20232024```javascript2025const formattedResults = formatResults(results.data);2026// Join the text content of all results2027const textSources = results.data2028  .map((result) => result.content.map((c) => c.text).join("\n"))2029  .join("\n");20302031const completion = await client.chat.completions.create({2032  model: "gpt-6-astra",2033  messages: [2034    {2035      role: "developer",2036      content:2037        "Produce a concise answer to the query based on the provided sources.",2038    },2039    {2040      role: "user",2041      content: `Sources: ${formattedResults}\n\nQuery: '${userQuery}'`,2042    },2043  ],2044});20452046console.log(completion.choices[0].message.content);2047```20482049```python2050# Use results and user_query from the preceding search step.2051formatted_results = format_results(results.data)20522053"\n".join("\n".join(c.text for c in result.content) for result in results.data)20542055completion = client.chat.completions.create(2056    model="gpt-6-astra",2057    messages=[2058        {2059            "role": "developer",2060            "content": "Produce a concise answer to the query based on the provided sources.",2061        },2062        {2063            "role": "user",2064            "content": f"Sources: {formatted_results}\n\nQuery: '{user_query}'",2065        },2066    ],2067)20682069print(completion.choices[0].message.content)2070```20712072```go2073package main20742075import (2076	"context"2077	"fmt"2078	"strings"20792080	"github.com/openai/openai-go/v3"2081)20822083func main() {2084	client := openai.NewClient()2085	userQuery := "What is the return policy?"2086	results, err := client.VectorStores.Search(context.Background(), "vs_123", openai.VectorStoreSearchParams{2087		Query: openai.VectorStoreSearchParamsQueryUnion{OfString: openai.String(userQuery)},2088	})2089	if err != nil {2090		panic(err)2091	}20922093	completion, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{2094		Model: "gpt-6-astra",2095		Messages: []openai.ChatCompletionMessageParamUnion{2096			openai.DeveloperMessage("Produce a concise answer to the query based on the provided sources."),2097			openai.UserMessage(fmt.Sprintf("Sources: %s\n\nQuery: %q", formatResults(results.Data), userQuery)),2098		},2099	})2100	if err != nil {2101		panic(err)2102	}2103	fmt.Println(completion.Choices[0].Message.Content)2104}21052106func formatResults(results []openai.VectorStoreSearchResponse) string {2107	var sources strings.Builder2108	sources.WriteString("<sources>")2109	for _, result := range results {2110		fmt.Fprintf(&sources, "<result file_id=%q file_name=%q>", result.FileID, result.Filename)2111		for _, content := range result.Content {2112			fmt.Fprintf(&sources, "<content>%s</content>", content.Text)2113		}2114		sources.WriteString("</result>")2115	}2116	sources.WriteString("</sources>")2117	return sources.String()2118}2119```21202121```java2122import com.openai.client.OpenAIClient;2123import com.openai.client.okhttp.OpenAIOkHttpClient;2124import com.openai.models.chat.completions.ChatCompletionCreateParams;2125import com.openai.models.vectorstores.VectorStoreSearchParams;2126import java.util.stream.Collectors;21272128String vectorStoreId = "vs_123";21292130String query = "What is the return policy?";2131var results =2132    client2133        .vectorStores()2134        .search(vectorStoreId, VectorStoreSearchParams.builder().query(query).build());2135String sources =2136    results.data().stream()2137        .map(2138            result ->2139                "<result file_id='"2140                    + result.fileId()2141                    + "' file_name='"2142                    + result.filename()2143                    + "'>"2144                    + result.content().stream()2145                        .map(content -> "<content>" + content.text() + "</content>")2146                        .collect(Collectors.joining())2147                    + "</result>")2148        .collect(Collectors.joining());21492150var completion =2151    client2152        .chat()2153        .completions()2154        .create(2155            ChatCompletionCreateParams.builder()2156                .model("gpt-6-astra")2157                .addDeveloperMessage(2158                    "Answer the query concisely using only the provided sources.")2159                .addUserMessage(2160                    "Sources: <sources>" + sources + "</sources>\n\nQuery: " + query)2161                .build());21622163completion.choices().stream()2164    .flatMap(choice -> choice.message().content().stream())2165    .forEach(System.out::println);2166```21672168```ruby2169require "openai"21702171client = OpenAI::Client.new2172query = "What is the return policy?"2173results = client.vector_stores.search("vs_123", query: query)2174sources = (results.data || []).map do |result|2175  content = result.content.map { |part| "<content>#{part.text}</content>" }.join2176  "<result file_id='#{result.file_id}' file_name='#{result.filename}'>#{content}</result>"2177end.join21782179completion = client.chat.completions.create(2180  model: "gpt-6-astra",2181  messages: [2182    {2183      role: :developer,2184      content: "Answer the query concisely using only the provided sources."2185    },2186    {2187      role: :user,2188      content: "Sources: <sources>#{sources}</sources>\n\nQuery: #{query}"2189    }2190  ]2191)2192puts(completion.choices.fetch(0).message.content)2193```219421952196```json2197"Our return policy allows returns within 30 days of purchase."2198```21992200This uses a sample `format_results` function, which could be implemented like2201so:22022203Sample result formatting function22042205```javascript2206function formatResults(results) {2207  let formattedResults = "";2208  for (const result of results.data) {2209    let formattedResult = `<result file_id='${result.file_id}' file_name='${result.filename}'>`;2210    for (const part of result.content) {2211      formattedResult += `<content>${part.text}</content>`;2212    }2213    formattedResults += formattedResult + "</result>";2214  }2215  return `<sources>${formattedResults}</sources>`;2216}2217```22182219```python2220def format_results(results):2221    formatted_results = ""2222    for result in results.data:2223        formatted_result = (2224            f"<result file_id='{result.file_id}' file_name='{result.file_name}'>"2225        )2226        for part in result.content:2227            formatted_result += f"<content>{part.text}</content>"2228        formatted_results += formatted_result + "</result>"2229    return f"<sources>{formatted_results}</sources>"2230```22312232```go2233package main22342235import (2236	"fmt"2237	"strings"22382239	"github.com/openai/openai-go/v3"2240)22412242func main() {2243	results := []openai.VectorStoreSearchResponse{{2244		FileID:   "file-12345",2245		Filename: "woodchuck_policy.txt",2246		Content:  []openai.VectorStoreSearchResponseContent{{Text: "Each passenger may carry up to two woodchucks."}},2247	}}2248	fmt.Println(formatResults(results))2249}22502251func formatResults(results []openai.VectorStoreSearchResponse) string {2252	var sources strings.Builder2253	sources.WriteString("<sources>")2254	for _, result := range results {2255		fmt.Fprintf(&sources, "<result file_id=%q file_name=%q>", result.FileID, result.Filename)2256		for _, content := range result.Content {2257			fmt.Fprintf(&sources, "<content>%s</content>", content.Text)2258		}2259		sources.WriteString("</result>")2260	}2261	sources.WriteString("</sources>")2262	return sources.String()2263}2264```22652266```ruby2267results = [2268  {2269    file_id: "file-12345",2270    filename: "woodchuck_policy.txt",2271    content: [{ text: "Each passenger may carry up to two woodchucks." }]2272  }2273]22742275sources = results.map do |result|2276  content = result.fetch(:content).map { |part| "<content>#{part.fetch(:text)}</content>" }.join2277  "<result file_id=\"#{result.fetch(:file_id)}\" file_name=\"#{result.fetch(:filename)}\">#{content}</result>"2278end22792280puts("<sources>#{sources.join}</sources>")2281```
Files main
Menu

Theme