-
Notifications
You must be signed in to change notification settings - Fork 3
docs: Adding fern python documentation #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,24 @@ | ||
from cohere.compass.clients import CompassClient | ||
from cohere.compass.models import DocumentAttributes | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
DOCUMENT_ID = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
doc_attr = DocumentAttributes() | ||
doc_attr.key = "value" | ||
|
||
error = compass_client.add_attributes( | ||
index_name=INDEX_NAME, | ||
document_id=DOCUMENT_ID, | ||
attributes=doc_attr, | ||
) | ||
|
||
if error: | ||
raise Exception(f"Failed to add attributes: {error}") |
This file contains hidden or 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,15 @@ | ||
from cohere.compass.clients import CompassClient | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
r = compass_client.create_index(index_name=INDEX_NAME) | ||
|
||
if r.error: | ||
raise Exception(f"Failed to create index: {r.error}") |
This file contains hidden or 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,16 @@ | ||
from cohere.compass.clients import CompassClient | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
DOCUMENT_ID = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
r = compass_client.delete_document(index_name=INDEX_NAME, document_id=DOCUMENT_ID) | ||
|
||
if r.error: | ||
raise Exception(f"Failed to delete doc: {r.error}") |
This file contains hidden or 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,15 @@ | ||
from cohere.compass.clients import CompassClient | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
r = compass_client.delete_index(index_name=INDEX_NAME) | ||
|
||
if r.error: | ||
raise Exception(f"Failed to delete index: {r.error}") |
45 changes: 45 additions & 0 deletions
45
examples/fern_documentation/python/core/direct_search_index.py
This file contains hidden or 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,45 @@ | ||
from typing import Optional | ||
|
||
from cohere.compass.clients import CompassClient | ||
from cohere.compass.models import RetrievedChunkExtended | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
DOCUMENT_ID = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
query = {"query": {"match_all": {}}} | ||
|
||
|
||
def call_direct_search_scroll( | ||
hits: list[RetrievedChunkExtended], | ||
scroll_id: Optional[str] = None, | ||
) -> list[RetrievedChunkExtended]: | ||
if hits is None: | ||
hits = [] | ||
|
||
if scroll_id is None: | ||
return hits | ||
|
||
results_scroll = compass_client.direct_search_scroll( | ||
scroll_id=scroll_id, | ||
scroll="1m", | ||
) | ||
|
||
hits.extend(results_scroll.hits) | ||
return call_direct_search_scroll(hits=hits, scroll_id=results.scroll_id) | ||
|
||
|
||
results = compass_client.direct_search( | ||
index_name=INDEX_NAME, | ||
query=query, | ||
scroll="1m", | ||
size=100, | ||
) | ||
|
||
hits = call_direct_search_scroll(hits=results.hits, scroll_id=results.scroll_id) |
This file contains hidden or 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,45 @@ | ||
import json | ||
|
||
from cohere.compass.clients import CompassClient | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
DOCUMENT_ID = ... | ||
SEARCH_QUERY = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
results = compass_client.search_chunks(index_name=INDEX_NAME, query=SEARCH_QUERY) | ||
if results.error: | ||
raise Exception(f"Failed to search chunks: {results.error}") | ||
|
||
hits = results.hits | ||
|
||
if not hits: | ||
raise Exception("No hits found") | ||
|
||
hit = hits[0] | ||
|
||
asset_id: str = hit.assets_info[0].asset_id # type: ignore | ||
document_id = hit.document_id | ||
# URL to fetch asset | ||
presigned_url = hit.assets_info[0].presigned_url | ||
|
||
### Get document asset again, after pre-signed url has expired | ||
asset, content_type = compass_client.get_document_asset( | ||
index_name=document_id, | ||
document_id=DOCUMENT_ID, | ||
asset_id=asset_id, | ||
) | ||
|
||
# Save the asset to a file. | ||
if content_type in ["image/jpeg", "image/png"]: | ||
with open(f"{asset_id}", "wb") as f: | ||
f.write(asset) | ||
# Print the asset as JSON | ||
elif "text/json" in content_type: | ||
print(json.dumps(asset, indent=2)) |
This file contains hidden or 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,18 @@ | ||
from cohere.compass.clients import CompassClient | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
DOCUMENT_ID = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
r = compass_client.get_document(index_name=INDEX_NAME, document_id=DOCUMENT_ID) | ||
|
||
if r.error: | ||
raise Exception(f"Failed to get doc: {r.error}") | ||
|
||
print(r.result.hits) |
This file contains hidden or 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,20 @@ | ||
from cohere.compass.clients import CompassClient | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
r = compass_client.list_indexes() | ||
if r.error: | ||
raise Exception(f"Failed to list indexes: {r.error}") | ||
|
||
indexes = r.result["indexes"] | ||
for index in indexes: | ||
print(f"Index name: {index['name']}") | ||
print(f"Chunk count: {index['count']}") | ||
print(f"Document count: {index['parent_doc_count']}") |
23 changes: 23 additions & 0 deletions
23
examples/fern_documentation/python/core/put_doc_into_index.py
This file contains hidden or 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,23 @@ | ||
from cohere.compass.clients import CompassClient, CompassParserClient | ||
|
||
COMPASS_API_URL = ... | ||
PARSER_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
FOLDER_TO_PROCESS = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
compass_parser_client = CompassParserClient(parser_url=PARSER_URL) | ||
|
||
docs_to_index = compass_parser_client.process_folder( | ||
folder_path=FOLDER_TO_PROCESS, recursive=True | ||
) | ||
|
||
compass_client.create_index(index_name=INDEX_NAME) | ||
r = compass_client.insert_docs(index_name=INDEX_NAME, docs=docs_to_index, num_jobs=1) | ||
|
||
print(r) | ||
This file contains hidden or 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,14 @@ | ||
from cohere.compass.clients import CompassClient | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
r = compass_client.refresh_index(index_name=INDEX_NAME) | ||
if r.error: | ||
raise Exception(f"Failed to refresh indexes: {r.error}") |
This file contains hidden or 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,19 @@ | ||
from cohere.compass.clients import CompassClient | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
DOCUMENT_ID = ... | ||
SEARCH_QUERY = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
r = compass_client.search_chunks(index_name=INDEX_NAME, query=SEARCH_QUERY) | ||
|
||
if r.error: | ||
raise Exception(f"Failed to search: {r.error}") | ||
|
||
print(r.hits) |
This file contains hidden or 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,19 @@ | ||
from cohere.compass.clients import CompassClient | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
DOCUMENT_ID = ... | ||
SEARCH_QUERY = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
r = compass_client.search_documents(index_name=INDEX_NAME, query=SEARCH_QUERY) | ||
|
||
if r.error: | ||
raise Exception(f"Failed to search doc: {r.error}") | ||
|
||
print(r.hits) |
28 changes: 28 additions & 0 deletions
28
examples/fern_documentation/python/core/update_group_auth.py
This file contains hidden or 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,28 @@ | ||
from cohere.compass import GroupAuthorizationInput | ||
from cohere.compass.clients import CompassClient | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
DOCUMENT_ID = ... | ||
AUTHORIZED_GROUP_NAME = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
r = compass_client.update_group_authorization( | ||
index_name=INDEX_NAME, | ||
group_auth_input=GroupAuthorizationInput( | ||
document_ids=[DOCUMENT_ID], | ||
authorized_groups=[AUTHORIZED_GROUP_NAME], | ||
action=GroupAuthorizationInput.Actions.ADD, | ||
), | ||
) | ||
|
||
for doc in r.results: | ||
if doc.error: | ||
print(f"Error processing doc: {doc.document_id}, error: {doc.error}") | ||
continue | ||
print(f"Successfully processed doc: {doc.document_id}") |
This file contains hidden or 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,37 @@ | ||
import uuid | ||
|
||
from cohere.compass.clients import CompassClient | ||
from cohere.compass.models import DocumentAttributes | ||
|
||
COMPASS_API_URL = ... | ||
BEARER_TOKEN = ... | ||
INDEX_NAME = ... | ||
DOCUMENT_ID = ... | ||
FILE_PATH = ... | ||
FILE_NAME = ... | ||
CONTENT_TYPE = ... | ||
|
||
compass_client = CompassClient( | ||
index_url=COMPASS_API_URL, | ||
bearer_token=BEARER_TOKEN, | ||
) | ||
|
||
doc_attr = DocumentAttributes() | ||
doc_attr.key = "value" | ||
|
||
file_path = FILE_PATH | ||
if not file_path: | ||
raise Exception("FILE_PATH is required") | ||
|
||
with open(file_path, "rb") as f: | ||
file_bytes = f.read() | ||
|
||
result = compass_client.upload_document( | ||
index_name=INDEX_NAME, | ||
filename=FILE_NAME, | ||
filebytes=file_bytes, | ||
content_type=CONTENT_TYPE, | ||
document_id=uuid.uuid4(), | ||
) | ||
|
||
print(result) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.