Skip to content

Commit 1c0bac2

Browse files
invader89Max Shkutnyktrentfowlercohere
authored
Format code samples (#257)
* format code samples, remove non-python entries from python code samples * auto-format more code snippets * Fix pip install snippets * rename check-mdx-frontmatter to cjs --------- Co-authored-by: Max Shkutnyk <max@lightsonsoftware.com> Co-authored-by: trentfowlercohere <141260477+trentfowlercohere@users.noreply.github.com>
1 parent 2a93e1a commit 1c0bac2

9 files changed

+65
-65
lines changed

.github/workflows/check-mdx-frontmatter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ jobs:
2626
run: pnpm install
2727

2828
- name: Run MDX frontmatter check
29-
run: node .github/scripts/check-mdx-frontmatter.js
29+
run: node .github/scripts/check-mdx-frontmatter.cjs

fern/pages/cookbooks/convfinqa-finetuning-wandb.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ from cohere.finetuning import (
5555
)
5656

5757
# fill in your Cohere API key here
58-
os.environ['COHERE_API_KEY'] = "<COHERE_API_KEY>"
58+
os.environ["COHERE_API_KEY"] = "<COHERE_API_KEY>"
5959

6060
# instantiate the Cohere client
61-
co = cohere.Client(os.environ['COHERE_API_KEY'])
61+
co = cohere.Client(os.environ["COHERE_API_KEY"])
6262
```
6363

6464
## Dataset

fern/pages/cookbooks/deploy-finetuned-model-aws-marketplace.mdx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ To subscribe to the algorithm:
7171
Install the Python packages you will use below and import them. For example, you can run the command below to install `cohere` if you haven't done so.
7272

7373

74-
```python
75-
!pip install "cohere>=5.11.0"
74+
```sh
75+
pip install "cohere>=5.11.0"
7676
```
7777

7878

@@ -200,9 +200,10 @@ save_hf_model(merged_weights_dir, merged_model)
200200

201201

202202
```python
203-
%%time
204203
sess = sage.Session()
205-
merged_weights = S3Uploader.upload(merged_weights_dir, s3_checkpoint_dir, sagemaker_session=sess)
204+
merged_weights = S3Uploader.upload(
205+
merged_weights_dir, s3_checkpoint_dir, sagemaker_session=sess
206+
)
206207
print("merged_weights", merged_weights)
207208
```
208209

@@ -213,7 +214,6 @@ Create Cohere client and use it to export the merged weights to the TensorRT-LLM
213214

214215

215216
```python
216-
%%time
217217
co = cohere.SagemakerClient(aws_region=region)
218218
co.sagemaker_finetuning.export_finetune(
219219
arn=arn,
@@ -232,7 +232,6 @@ The Cohere client provides a built-in method to create an endpoint for inference
232232

233233

234234
```python
235-
%%time
236235
co.sagemaker_finetuning.create_endpoint(
237236
arn=arn,
238237
endpoint_name=endpoint_name,

fern/pages/cookbooks/finetune-on-sagemaker.mdx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ To subscribe to the model algorithm:
5858
2. On the AWS Marketplace listing, click on the **Continue to Subscribe** button.
5959
3. On the **Subscribe to this software** page, review and click on **"Accept Offer"** if you and your organization agrees with EULA, pricing, and support terms. On the "Configure and launch" page, make sure ARN displayed in your region match with the ARN in the following cell.
6060

61+
```sh
62+
pip install "cohere>=5.11.0"
63+
```
6164

6265
```python
63-
!pip install "cohere>=5.11.0"
64-
6566
import cohere
6667
import boto3
6768
import sagemaker as sage
@@ -297,7 +298,7 @@ from tqdm import tqdm
297298
total = 0
298299
correct = 0
299300
for line in tqdm(
300-
open('./sample_finetune_scienceQA_eval.jsonl').readlines()
301+
open("./sample_finetune_scienceQA_eval.jsonl").readlines()
301302
):
302303
total += 1
303304
question_answer_json = json.loads(line)

fern/pages/cookbooks/rag-cohere-mongodb.mdx

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ Libraries:
5252

5353

5454

55-
```python
56-
!pip install --quiet datasets tqdm cohere pymongo
55+
```sh
56+
pip install --quiet datasets tqdm cohere pymongo
5757
```
5858

5959

@@ -183,11 +183,11 @@ def combine_attributes(row):
183183
combined = f"{row['company']} {row['sector']} "
184184

185185
# Add reports information
186-
for report in row['reports']:
186+
for report in row["reports"]:
187187
combined += f"{report['year']} {report['title']} {report['author']} {report['content']} "
188188

189189
# Add recent news information
190-
for news in row['recent_news']:
190+
for news in row["recent_news"]:
191191
combined += f"{news['headline']} {news['summary']} "
192192

193193
return combined.strip()
@@ -196,15 +196,15 @@ def combine_attributes(row):
196196

197197
```python
198198
# Add the new column 'combined_attributes'
199-
dataset_df['combined_attributes'] = dataset_df.apply(
199+
dataset_df["combined_attributes"] = dataset_df.apply(
200200
combine_attributes, axis=1
201201
)
202202
```
203203

204204

205205
```python
206206
# Display the first few rows of the updated dataframe
207-
dataset_df[['company', 'ticker', 'combined_attributes']].head()
207+
dataset_df[["company", "ticker", "combined_attributes"]].head()
208208
```
209209

210210
<div>
@@ -270,7 +270,7 @@ def get_embedding(
270270
texts=[text],
271271
model=model,
272272
input_type=input_type, # Used for embeddings of search queries run against a vector DB to find relevant documents
273-
embedding_types=['float'],
273+
embedding_types=["float"],
274274
)
275275

276276
return response.embeddings.float[0]
@@ -279,7 +279,7 @@ def get_embedding(
279279
# Apply the embedding function with a progress bar
280280
tqdm.pandas(desc="Generating embeddings")
281281
dataset_df["embedding"] = dataset_df[
282-
'combined_attributes'
282+
"combined_attributes"
283283
].progress_apply(get_embedding)
284284

285285
print(f"We just computed {len(dataset_df['embedding'])} embeddings.")
@@ -421,8 +421,8 @@ def get_mongo_client(mongo_uri):
421421
)
422422

423423
# Validate the connection
424-
ping_result = client.admin.command('ping')
425-
if ping_result.get('ok') == 1.0:
424+
ping_result = client.admin.command("ping")
425+
if ping_result.get("ok") == 1.0:
426426
# Connection successful
427427
print("Connection to MongoDB successful")
428428
return client
@@ -478,7 +478,7 @@ MongoDB's Document model and its compatibility with Python dictionaries offer se
478478
![](../../assets/images/rag-cohere-mongodb-4.png)
479479

480480
```python
481-
documents = dataset_df.to_dict('records')
481+
documents = dataset_df.to_dict("records")
482482
collection.insert_many(documents)
483483

484484
print("Data ingestion into MongoDB completed")
@@ -592,13 +592,13 @@ def rerank_documents(query: str, documents, top_n: int = 3):
592592
original_doc = documents[result.index]
593593
top_documents_after_rerank.append(
594594
{
595-
'company': original_doc['company'],
596-
'combined_attributes': original_doc[
597-
'combined_attributes'
595+
"company": original_doc["company"],
596+
"combined_attributes": original_doc[
597+
"combined_attributes"
598598
],
599-
'reports': original_doc['reports'],
600-
'vector_search_score': original_doc['score'],
601-
'relevance_score': result.relevance_score,
599+
"reports": original_doc["reports"],
600+
"vector_search_score": original_doc["score"],
601+
"relevance_score": result.relevance_score,
602602
}
603603
)
604604

@@ -724,9 +724,9 @@ pd.DataFrame(reranked_documents).head()
724724
def format_documents_for_chat(documents):
725725
return [
726726
{
727-
"company": doc['company'],
727+
"company": doc["company"],
728728
# "reports": doc['reports'],
729-
"combined_attributes": doc['combined_attributes'],
729+
"combined_attributes": doc["combined_attributes"],
730730
}
731731
for doc in documents
732732
]
@@ -825,7 +825,7 @@ class CohereChat:
825825
# Use the connection string from history_params
826826
self.client = pymongo.MongoClient(
827827
self.history_params.get(
828-
'connection_string', 'mongodb://localhost:27017/'
828+
"connection_string", "mongodb://localhost:27017/"
829829
)
830830
)
831831

@@ -838,34 +838,34 @@ class CohereChat:
838838
# Use the history_collection from history_params, or default to "chat_history"
839839
self.history_collection = self.db[
840840
self.history_params.get(
841-
'history_collection', 'chat_history'
841+
"history_collection", "chat_history"
842842
)
843843
]
844844

845845
# Use the session_id from history_params, or default to "default_session"
846846
self.session_id = self.history_params.get(
847-
'session_id', 'default_session'
847+
"session_id", "default_session"
848848
)
849849

850850
def add_to_history(self, message: str, prefix: str = ""):
851851
self.history_collection.insert_one(
852852
{
853-
'session_id': self.session_id,
854-
'message': message,
855-
'prefix': prefix,
853+
"session_id": self.session_id,
854+
"message": message,
855+
"prefix": prefix,
856856
}
857857
)
858858

859859
def get_chat_history(self) -> List[Dict[str, str]]:
860860
history = self.history_collection.find(
861-
{'session_id': self.session_id}
862-
).sort('_id', 1)
861+
{"session_id": self.session_id}
862+
).sort("_id", 1)
863863
return [
864864
{
865865
"role": (
866-
"user" if item['prefix'] == "USER" else "chatbot"
866+
"user" if item["prefix"] == "USER" else "chatbot"
867867
),
868-
"message": item['message'],
868+
"message": item["message"],
869869
}
870870
for item in history
871871
]
@@ -875,11 +875,11 @@ class CohereChat:
875875
) -> List[Dict]:
876876
rerank_docs = [
877877
{
878-
'company': doc['company'],
879-
'combined_attributes': doc['combined_attributes'],
878+
"company": doc["company"],
879+
"combined_attributes": doc["combined_attributes"],
880880
}
881881
for doc in documents
882-
if doc['combined_attributes'].strip()
882+
if doc["combined_attributes"].strip()
883883
]
884884

885885
if not rerank_docs:
@@ -897,11 +897,11 @@ class CohereChat:
897897

898898
top_documents_after_rerank = [
899899
{
900-
'company': rerank_docs[result.index]['company'],
901-
'combined_attributes': rerank_docs[result.index][
902-
'combined_attributes'
900+
"company": rerank_docs[result.index]["company"],
901+
"combined_attributes": rerank_docs[result.index][
902+
"combined_attributes"
903903
],
904-
'relevance_score': result.relevance_score,
904+
"relevance_score": result.relevance_score,
905905
}
906906
for result in response.results
907907
]
@@ -925,8 +925,8 @@ class CohereChat:
925925
) -> List[Dict]:
926926
return [
927927
{
928-
"company": doc['company'],
929-
"combined_attributes": doc['combined_attributes'],
928+
"company": doc["company"],
929+
"combined_attributes": doc["combined_attributes"],
930930
}
931931
for doc in documents
932932
]
@@ -972,8 +972,8 @@ class CohereChat:
972972

973973
def show_history(self):
974974
history = self.history_collection.find(
975-
{'session_id': self.session_id}
976-
).sort('_id', 1)
975+
{"session_id": self.session_id}
976+
).sort("_id", 1)
977977
for item in history:
978978
print(f"{item['prefix']}: {item['message']}")
979979
print("-------------------------")
@@ -988,9 +988,9 @@ chat = CohereChat(
988988
database=DB_NAME,
989989
main_collection=COLLECTION_NAME,
990990
history_params={
991-
'connection_string': MONGO_URI,
992-
'history_collection': "chat_history",
993-
'session_id': 2,
991+
"connection_string": MONGO_URI,
992+
"history_collection": "chat_history",
993+
"session_id": 2,
994994
},
995995
)
996996

fern/pages/v2/text-generation/retrieval-augmented-generation-rag.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ documents = [
5656
"title": "What are animals?",
5757
"snippet": "Animals are different from plants.",
5858
}
59-
}
59+
},
6060
]
6161

6262
# Add the user message
@@ -66,7 +66,7 @@ messages = [{"role": "user", "content": message}]
6666
response = co.chat(
6767
model="command-r-plus-08-2024",
6868
messages=messages,
69-
documents=documents
69+
documents=documents,
7070
)
7171

7272
print(response.message.content[0].text)
@@ -246,7 +246,7 @@ messages = [{"role": "user", "content": message}]
246246
response = co.chat(
247247
model="command-r-plus-08-2024",
248248
messages=messages,
249-
documents=documents
249+
documents=documents,
250250
)
251251

252252
print(response.message.content[0].text)

fern/pages/v2/text-generation/structured-outputs-json.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ res = co.chat(
3535
"content": "Generate a JSON describing a person, with the fields 'name' and 'age'",
3636
}
3737
],
38-
response_format={"type": "json_object"}
38+
response_format={"type": "json_object"},
3939
)
4040

4141
print(res.message.content[0].text)
@@ -86,7 +86,7 @@ res = co.chat(
8686
"publication_year": {"type": "integer"},
8787
},
8888
},
89-
}
89+
},
9090
)
9191

9292
print(res.message.content[0].text)

fern/pages/v2/tutorials/agentic-rag/generating-multi-faceted-queries.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ search_code_examples_detailed_tool = {
9494
"properties": {
9595
"query": {
9696
"type": "string",
97-
"description": "The search query."
97+
"description": "The search query.",
9898
},
9999
"programming_language": {
100100
"type": "string",
@@ -104,11 +104,11 @@ search_code_examples_detailed_tool = {
104104
"type": "array",
105105
"items": {"type": "string"},
106106
"description": "The Cohere endpoints used in the code example or tutorial. Only use this property when asked by the user. Possible enum values: chat, embed, rerank, classify.",
107-
}
107+
},
108108
},
109-
"required": ["query"]
110-
}
111-
}
109+
"required": ["query"],
110+
},
111+
},
112112
}
113113
```
114114
```python PYTHON

0 commit comments

Comments
 (0)