From 05b391dbc81d293620f71edf1100324ec6196cee Mon Sep 17 00:00:00 2001 From: Max Shkutnyk Date: Tue, 18 Feb 2025 02:09:25 +0200 Subject: [PATCH] Fixing issue with format code snippets pipeline (#412) * testing workflow trigger * testing workflow trigger * testing workflow trigger * testing workflow trigger * testing workflow trigger * testing workflow trigger * reformat python code examples --------- Co-authored-by: Max Shkutnyk --- .../workflows/check-python-code-snippets.yml | 9 +- .../private-deployment-usage.mdx | 3 +- .../get-started/quickstart/rag-quickstart.mdx | 24 +- .../quickstart/reranking-quickstart.mdx | 30 +-- .../quickstart/sem-search-quickstart.mdx | 38 ++-- .../quickstart/text-gen-quickstart.mdx | 205 ++++++++---------- .../quickstart/tool-use-quickstart.mdx | 75 +++---- .../private-deployment-usage.mdx | 3 +- .../get-started/quickstart/rag-quickstart.mdx | 20 +- .../quickstart/reranking-quickstart.mdx | 28 +-- .../quickstart/sem-search-quickstart.mdx | 35 +-- .../quickstart/text-gen-quickstart.mdx | 98 ++++----- .../quickstart/tool-use-quickstart.mdx | 103 ++++----- .../cohere-on-azure/azure-ai-rag.mdx | 3 +- 14 files changed, 304 insertions(+), 370 deletions(-) diff --git a/.github/workflows/check-python-code-snippets.yml b/.github/workflows/check-python-code-snippets.yml index 005738e4a..8ccc231d6 100644 --- a/.github/workflows/check-python-code-snippets.yml +++ b/.github/workflows/check-python-code-snippets.yml @@ -43,11 +43,8 @@ jobs: run: | git diff --exit-code || echo "::set-output name=changes::true" - - name: Commit and Push Changes + - name: Fail if changes are detected if: steps.diff.outputs.changes == 'true' run: | - git config --local user.email "action@github.com" - git config --local user.name "GitHub Action" - git add -u - git commit -m "Format Python snippets in MDX files" - git push \ No newline at end of file + echo "Changes in python code snippets are detected. Please run check-python-code-snippets locally and commit the changes." + exit 1 diff --git a/fern/pages/deployment-options/private-deployment/private-deployment-usage.mdx b/fern/pages/deployment-options/private-deployment/private-deployment-usage.mdx index a1c5a046e..b7ccea392 100644 --- a/fern/pages/deployment-options/private-deployment/private-deployment-usage.mdx +++ b/fern/pages/deployment-options/private-deployment/private-deployment-usage.mdx @@ -66,8 +66,7 @@ The only difference between using Cohere's models on private deployments and the import cohere co = cohere.Client( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` diff --git a/fern/pages/get-started/quickstart/rag-quickstart.mdx b/fern/pages/get-started/quickstart/rag-quickstart.mdx index 237e95dea..00e084791 100644 --- a/fern/pages/get-started/quickstart/rag-quickstart.mdx +++ b/fern/pages/get-started/quickstart/rag-quickstart.mdx @@ -29,7 +29,9 @@ Next, import the library and create a client. ```python PYTHON import cohere -co = cohere.Client("COHERE_API_KEY") # Get your free API key here: https://dashboard.cohere.com/api-keys +co = cohere.Client( + "COHERE_API_KEY" +) # Get your free API key here: https://dashboard.cohere.com/api-keys ``` @@ -39,8 +41,7 @@ co = cohere.Client("COHERE_API_KEY") # Get your free API key here: https://dashb import cohere co = cohere.Client( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` @@ -118,7 +119,7 @@ message = "Are there fitness-related benefits?" response = co.chat( model="command-r-plus-08-2024", message=message, - documents=documents + documents=documents, ) print(response.text) @@ -132,7 +133,7 @@ message = "Are there fitness-related benefits?" response = co.chat( model="command-r-plus-08-2024", message=message, - documents=documents + documents=documents, ) print(response.text) @@ -144,9 +145,7 @@ print(response.text) message = "Are there fitness-related benefits?" response = co.chat( - model="YOUR_MODEL_NAME", - message=message, - documents=documents + model="YOUR_MODEL_NAME", message=message, documents=documents ) print(response.text) @@ -158,9 +157,7 @@ print(response.text) message = "Are there fitness-related benefits?" response = co.chat( - model="YOUR_ENDPOINT_NAME", - message=message, - documents=documents + model="YOUR_ENDPOINT_NAME", message=message, documents=documents ) print(response.text) @@ -171,10 +168,7 @@ print(response.text) ```python PYTHON message = "Are there fitness-related benefits?" -response = co.chat( - message=message, - documents=documents -) +response = co.chat(message=message, documents=documents) print(response.text) ``` diff --git a/fern/pages/get-started/quickstart/reranking-quickstart.mdx b/fern/pages/get-started/quickstart/reranking-quickstart.mdx index bf0882d23..997dd4605 100644 --- a/fern/pages/get-started/quickstart/reranking-quickstart.mdx +++ b/fern/pages/get-started/quickstart/reranking-quickstart.mdx @@ -26,7 +26,9 @@ Next, import the library and create a client. ```python PYTHON import cohere -co = cohere.Client("COHERE_API_KEY") # Get your free API key here: https://dashboard.cohere.com/api-keys +co = cohere.Client( + "COHERE_API_KEY" +) # Get your free API key here: https://dashboard.cohere.com/api-keys ``` @@ -36,8 +38,7 @@ co = cohere.Client("COHERE_API_KEY") # Get your free API key here: https://dashb import cohere co = cohere.Client( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` @@ -62,10 +63,7 @@ co = cohere.SagemakerClient( ```python PYTHON import cohere -co = cohere.Client( - api_key="AZURE_API_KEY", - base_url="AZURE_ENDPOINT" -) +co = cohere.Client(api_key="AZURE_API_KEY", base_url="AZURE_ENDPOINT") ``` @@ -96,10 +94,7 @@ query = "Are there fitness-related perks?" # Rerank the documents results = co.rerank( - model="rerank-v3.5", - query=query, - documents=documents, - top_n=2 + model="rerank-v3.5", query=query, documents=documents, top_n=2 ) for result in results.results: @@ -114,10 +109,7 @@ query = "Are there fitness-related perks?" # Rerank the documents results = co.rerank( - model="rerank-v3.5", - query=query, - documents=documents, - top_n=2 + model="rerank-v3.5", query=query, documents=documents, top_n=2 ) for result in results.results: @@ -135,7 +127,7 @@ results = co.rerank( model="YOUR_ENDPOINT_NAME", query=query, documents=documents, - top_n=2 + top_n=2, ) for result in results.results: @@ -149,11 +141,7 @@ for result in results.results: query = "Are there fitness-related perks?" # Rerank the documents -results = co.rerank( - query=query, - documents=documents, - top_n=2 -) +results = co.rerank(query=query, documents=documents, top_n=2) for result in results.results: print(result) diff --git a/fern/pages/get-started/quickstart/sem-search-quickstart.mdx b/fern/pages/get-started/quickstart/sem-search-quickstart.mdx index 15136e6ed..9c7c89805 100644 --- a/fern/pages/get-started/quickstart/sem-search-quickstart.mdx +++ b/fern/pages/get-started/quickstart/sem-search-quickstart.mdx @@ -28,7 +28,9 @@ Next, import the library and create a client. ```python PYTHON import cohere -co = cohere.Client("COHERE_API_KEY") # Get your free API key here: https://dashboard.cohere.com/api-keys +co = cohere.Client( + "COHERE_API_KEY" +) # Get your free API key here: https://dashboard.cohere.com/api-keys ``` @@ -38,8 +40,7 @@ co = cohere.Client("COHERE_API_KEY") # Get your free API key here: https://dashb import cohere co = cohere.Client( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` @@ -81,8 +82,7 @@ co = cohere.SagemakerClient( import cohere co = cohere.Client( - api_key="AZURE_API_KEY_COMMAND", - base_url="AZURE_ENDPOINT_COMMAND" + api_key="AZURE_API_KEY_COMMAND", base_url="AZURE_ENDPOINT_COMMAND" ) ``` @@ -107,7 +107,7 @@ doc_emb = co.embed( model="embed-english-v3.0", input_type="search_document", texts=documents, - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -126,7 +126,7 @@ doc_emb = co.embed( model="embed-english-v3.0", input_type="search_document", texts=documents, - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -145,7 +145,7 @@ doc_emb = co.embed( model="YOUR_MODEL_NAME", input_type="search_document", texts=documents, - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -164,7 +164,7 @@ doc_emb = co.embed( model="YOUR_ENDPOINT_NAME", input_type="search_document", texts=documents, - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -182,7 +182,7 @@ documents = [ doc_emb = co.embed( input_type="search_document", texts=documents, - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -202,7 +202,7 @@ query_emb = co.embed( model="embed-english-v3.0", input_type="search_query", texts=[query], - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -217,7 +217,7 @@ query_emb = co.embed( model="embed-english-v3.0", input_type="search_query", texts=[query], - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -232,7 +232,7 @@ query_emb = co.embed( model="YOUR_MODEL_NAME", input_type="search_query", texts=[query], - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -247,7 +247,7 @@ query_emb = co.embed( model="YOUR_ENDPOINT_NAME", input_type="search_query", texts=[query], - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -261,7 +261,7 @@ query = "Ways to connect with my teammates" query_emb = co.embed( input_type="search_query", texts=[query], - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -274,17 +274,19 @@ Then, perform semantic search by computing the similarity between the query embe ```python PYTHON import numpy as np + # Compute dot product similarity and display results def return_results(query_emb, doc_emb, documents): - n = 2 # customize your top N results + n = 2 # customize your top N results scores = np.dot(query_emb, np.transpose(doc_emb))[0] max_idx = np.argsort(-scores)[:n] - + for rank, idx in enumerate(max_idx): print(f"Rank: {rank+1}") print(f"Score: {scores[idx]}") print(f"Document: {documents[idx]}\n") - + + return_results(query_emb, doc_emb, documents) ``` ```mdx wordWrap diff --git a/fern/pages/get-started/quickstart/text-gen-quickstart.mdx b/fern/pages/get-started/quickstart/text-gen-quickstart.mdx index 5645dfeb8..1d264702c 100644 --- a/fern/pages/get-started/quickstart/text-gen-quickstart.mdx +++ b/fern/pages/get-started/quickstart/text-gen-quickstart.mdx @@ -27,7 +27,9 @@ Next, import the library and create a client. ```python PYTHON import cohere -co = cohere.Client("COHERE_API_KEY") # Get your free API key here: https://dashboard.cohere.com/api-keys +co = cohere.Client( + "COHERE_API_KEY" +) # Get your free API key here: https://dashboard.cohere.com/api-keys ``` @@ -37,8 +39,7 @@ co = cohere.Client("COHERE_API_KEY") # Get your free API key here: https://dashb import cohere co = cohere.Client( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` @@ -97,7 +98,7 @@ To perform a basic text generation, call the Chat endpoint by passing the `messa ```python PYTHON response = co.chat( model="command-r-plus-08-2024", - message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?" + message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?", ) print(response.text) @@ -118,7 +119,7 @@ print(response.text) ```python PYTHON response = co.chat( model="YOUR_MODEL_NAME", - message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?" + message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?", ) print(response.text) @@ -129,7 +130,7 @@ print(response.text) ```python PYTHON response = co.chat( model="YOUR_ENDPOINT_NAME", - message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?" + message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?", ) print(response.text) @@ -156,111 +157,96 @@ To maintain the state of a conversation, such as for building chatbots, append a - ```python PYTHON - response = co.chat( - model="command-r-plus-08-2024", - preamble="You respond in concise sentences.", - chat_history=[ - { - "role": "user", - "message": "Hello" - }, - { - "role": "chatbot", - "message": "Hi, how can I help you today?" - } - ], - message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?" - ) - - print(response.text) - ``` +```python PYTHON +response = co.chat( + model="command-r-plus-08-2024", + preamble="You respond in concise sentences.", + chat_history=[ + {"role": "user", "message": "Hello"}, + { + "role": "chatbot", + "message": "Hi, how can I help you today?", + }, + ], + message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?", +) + +print(response.text) +``` - ```python PYTHON - response = co.chat( - preamble="You respond in concise sentences.", - chat_history=[ - { - "role": "user", - "message": "Hello" - }, - { - "role": "chatbot", - "message": "Hi, how can I help you today?" - } - ], - message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?" - ) - - print(response.text) - ``` +```python PYTHON +response = co.chat( + preamble="You respond in concise sentences.", + chat_history=[ + {"role": "user", "message": "Hello"}, + { + "role": "chatbot", + "message": "Hi, how can I help you today?", + }, + ], + message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?", +) + +print(response.text) +``` - ```python PYTHON - response = co.chat( - model="YOUR_MODEL_NAME", - preamble="You respond in concise sentences.", - chat_history=[ - { - "role": "user", - "message": "Hello" - }, - { - "role": "chatbot", - "message": "Hi, how can I help you today?" - } - ], - message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?" - ) - - print(response.text) - ``` +```python PYTHON +response = co.chat( + model="YOUR_MODEL_NAME", + preamble="You respond in concise sentences.", + chat_history=[ + {"role": "user", "message": "Hello"}, + { + "role": "chatbot", + "message": "Hi, how can I help you today?", + }, + ], + message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?", +) + +print(response.text) +``` - ```python PYTHON - response = co.chat( - model="YOUR_ENDPOINT_NAME", - preamble="You respond in concise sentences.", - chat_history=[ - { - "role": "user", - "message": "Hello" - }, - { - "role": "chatbot", - "message": "Hi, how can I help you today?" - } - ], - message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?" - ) - - print(response.text) - ``` +```python PYTHON +response = co.chat( + model="YOUR_ENDPOINT_NAME", + preamble="You respond in concise sentences.", + chat_history=[ + {"role": "user", "message": "Hello"}, + { + "role": "chatbot", + "message": "Hi, how can I help you today?", + }, + ], + message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?", +) + +print(response.text) +``` - ```python PYTHON - response = co.chat( - preamble="You respond in concise sentences.", - chat_history=[ - { - "role": "user", - "message": "Hello" - }, - { - "role": "chatbot", - "message": "Hi, how can I help you today?" - } - ], - message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?" - ) - - print(response.text) - ``` +```python PYTHON +response = co.chat( + preamble="You respond in concise sentences.", + chat_history=[ + {"role": "user", "message": "Hello"}, + { + "role": "chatbot", + "message": "Hi, how can I help you today?", + }, + ], + message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?", +) + +print(response.text) +``` ```mdx wordWrap @@ -277,8 +263,7 @@ To stream the generated text, call the Chat endpoint using `chat_stream` instead message = "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates." response = co.chat_stream( - model="command-r-plus-08-2024", - message=message + model="command-r-plus-08-2024", message=message ) for chunk in response: @@ -291,9 +276,7 @@ for chunk in response: ```python PYTHON message = "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates." -response = co.chat_stream( - message=message -) +response = co.chat_stream(message=message) for chunk in response: if chunk.event_type == "text-generation": @@ -305,10 +288,7 @@ for chunk in response: ```python PYTHON message = "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates." -response = co.chat_stream( - model="YOUR_MODEL_NAME", - message=message -) +response = co.chat_stream(model="YOUR_MODEL_NAME", message=message) for chunk in response: if chunk.event_type == "text-generation": @@ -320,10 +300,7 @@ for chunk in response: ```python PYTHON message = "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates." -response = co.chat_stream( - model="YOUR_ENDPOINT_NAME", - message=message -) +response = co.chat_stream(model="YOUR_ENDPOINT_NAME", message=message) for chunk in response: if chunk.event_type == "text-generation": @@ -335,9 +312,7 @@ for chunk in response: ```python PYTHON message = "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates." -response = co.chat_stream( - message=message -) +response = co.chat_stream(message=message) for chunk in response: if chunk.event_type == "text-generation": diff --git a/fern/pages/get-started/quickstart/tool-use-quickstart.mdx b/fern/pages/get-started/quickstart/tool-use-quickstart.mdx index e875c252b..cd603d8cb 100644 --- a/fern/pages/get-started/quickstart/tool-use-quickstart.mdx +++ b/fern/pages/get-started/quickstart/tool-use-quickstart.mdx @@ -29,7 +29,9 @@ Next, import the library and create a client. ```python PYTHON import cohere -co = cohere.Client("COHERE_API_KEY") # Get your free API key here: https://dashboard.cohere.com/api-keys +co = cohere.Client( + "COHERE_API_KEY" +) # Get your free API key here: https://dashboard.cohere.com/api-keys ``` @@ -39,8 +41,7 @@ co = cohere.Client("COHERE_API_KEY") # Get your free API key here: https://dashb import cohere co = cohere.Client( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` @@ -97,22 +98,23 @@ We also need to define the tool schemas in a format that can be passed to the Ch ```python PYTHON def get_weather(location): - # Implement your tool calling logic here + # Implement your tool calling logic here return {"temperature": "20C"} - + + functions_map = {"get_weather": get_weather} tools = [ { - "name": "get_weather", - "description": "Gets the weather of a given location", - "parameter_definitions": { - "location": { - "description": "The location to get weather, example: San Francisco, CA", - "type": "str", - "required": True - } - } + "name": "get_weather", + "description": "Gets the weather of a given location", + "parameter_definitions": { + "location": { + "description": "The location to get weather, example: San Francisco, CA", + "type": "str", + "required": True, + } + }, }, ] ``` @@ -128,9 +130,7 @@ The LLM will then generate the tool calls (if any) and return the `tool_calls` o message = "What's the weather in Toronto?" response = co.chat( - model="command-r-plus-08-2024", - message=message, - tools=tools + model="command-r-plus-08-2024", message=message, tools=tools ) print(response.tool_calls) @@ -142,9 +142,7 @@ print(response.tool_calls) message = "What's the weather in Toronto?" response = co.chat( - model="command-r-plus-08-2024", - message=message, - tools=tools + model="command-r-plus-08-2024", message=message, tools=tools ) print(response.tool_calls) @@ -156,9 +154,7 @@ print(response.tool_calls) message = "What's the weather in Toronto?" response = co.chat( - model="YOUR_MODEL_NAME", - message=message, - tools=tools + model="YOUR_MODEL_NAME", message=message, tools=tools ) print(response.tool_calls) @@ -170,9 +166,7 @@ print(response.tool_calls) message = "What's the weather in Toronto?" response = co.chat( - model="YOUR_ENDPOINT_NAME", - message=message, - tools=tools + model="YOUR_ENDPOINT_NAME", message=message, tools=tools ) print(response.tool_calls) @@ -183,10 +177,7 @@ print(response.tool_calls) ```python PYTHON message = "What's the weather in Toronto?" -response = co.chat( - message=message, - tools=tools -) +response = co.chat(message=message, tools=tools) print(response.tool_calls) ``` @@ -206,7 +197,9 @@ if response.tool_calls: for tc in response.tool_calls: tool_call = {"name": tc.name, "parameters": tc.parameters} tool_result = functions_map[tc.name](**tc.parameters) - tool_content.append({"call": tool_call, "outputs": [tool_result]}) + tool_content.append( + {"call": tool_call, "outputs": [tool_result]} + ) ``` ### Response Generation @@ -219,8 +212,8 @@ response = co.chat( model="command-r-plus-08-2024", message="", tools=tools, - tool_results=tool_content, - chat_history=response.chat_history + tool_results=tool_content, + chat_history=response.chat_history, ) print(response.text) @@ -233,8 +226,8 @@ response = co.chat( model="command-r-plus-08-2024", message="", tools=tools, - tool_results=tool_content, - chat_history=response.chat_history + tool_results=tool_content, + chat_history=response.chat_history, ) print(response.text) @@ -247,8 +240,8 @@ response = co.chat( model="YOUR_MODEL_NAME", message="", tools=tools, - tool_results=tool_content, - chat_history=response.chat_history + tool_results=tool_content, + chat_history=response.chat_history, ) print(response.text) @@ -261,8 +254,8 @@ response = co.chat( model="YOUR_ENDPOINT_NAME", message="", tools=tools, - tool_results=tool_content, - chat_history=response.chat_history + tool_results=tool_content, + chat_history=response.chat_history, ) print(response.text) @@ -274,8 +267,8 @@ print(response.text) response = co.chat( message="", tools=tools, - tool_results=tool_content, - chat_history=response.chat_history + tool_results=tool_content, + chat_history=response.chat_history, ) print(response.text) diff --git a/fern/pages/v2/deployment-options/private-deployment/private-deployment-usage.mdx b/fern/pages/v2/deployment-options/private-deployment/private-deployment-usage.mdx index 894328223..7c45d880c 100644 --- a/fern/pages/v2/deployment-options/private-deployment/private-deployment-usage.mdx +++ b/fern/pages/v2/deployment-options/private-deployment/private-deployment-usage.mdx @@ -66,8 +66,7 @@ The only difference between using Cohere's models on private deployments and the import cohere co = cohere.ClientV2( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` diff --git a/fern/pages/v2/get-started/quickstart/rag-quickstart.mdx b/fern/pages/v2/get-started/quickstart/rag-quickstart.mdx index 3e5e9d5f3..3aa17322d 100644 --- a/fern/pages/v2/get-started/quickstart/rag-quickstart.mdx +++ b/fern/pages/v2/get-started/quickstart/rag-quickstart.mdx @@ -28,7 +28,9 @@ Next, import the library and create a client. ```python PYTHON import cohere -co = cohere.ClientV2("COHERE_API_KEY") # Get your free API key here: https://dashboard.cohere.com/api-keys +co = cohere.ClientV2( + "COHERE_API_KEY" +) # Get your free API key here: https://dashboard.cohere.com/api-keys ``` @@ -38,8 +40,7 @@ co = cohere.ClientV2("COHERE_API_KEY") # Get your free API key here: https://das import cohere co = cohere.ClientV2( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` @@ -113,7 +114,6 @@ documents = [ } }, ] - ``` ### Response Generation @@ -129,7 +129,7 @@ query = "Are there health benefits?" response = co.chat( model="command-r-plus-08-2024", messages=[{"role": "user", "content": query}], - documents=documents + documents=documents, ) # Display the response @@ -146,7 +146,7 @@ query = "Are there health benefits?" response = co.chat( model="command-r-plus-08-2024", messages=[{"role": "user", "content": query}], - documents=documents + documents=documents, ) # Display the response @@ -163,7 +163,7 @@ query = "Are there health benefits?" response = co.chat( model="YOUR_MODEL_NAME", messages=[{"role": "user", "content": query}], - documents=documents + documents=documents, ) # Display the response @@ -180,7 +180,7 @@ query = "Are there health benefits?" response = co.chat( model="YOUR_ENDPOINT_NAME", messages=[{"role": "user", "content": query}], - documents=documents + documents=documents, ) # Display the response @@ -195,9 +195,9 @@ query = "Are there health benefits?" # Generate the response response = co.chat( - model="model", # Pass a dummy string + model="model", # Pass a dummy string messages=[{"role": "user", "content": query}], - documents=documents + documents=documents, ) # Display the response diff --git a/fern/pages/v2/get-started/quickstart/reranking-quickstart.mdx b/fern/pages/v2/get-started/quickstart/reranking-quickstart.mdx index a6af14c89..49cdde347 100644 --- a/fern/pages/v2/get-started/quickstart/reranking-quickstart.mdx +++ b/fern/pages/v2/get-started/quickstart/reranking-quickstart.mdx @@ -26,7 +26,9 @@ Next, import the library and create a client. ```python PYTHON import cohere -co = cohere.ClientV2("COHERE_API_KEY") # Get your free API key here: https://dashboard.cohere.com/api-keys +co = cohere.ClientV2( + "COHERE_API_KEY" +) # Get your free API key here: https://dashboard.cohere.com/api-keys ``` @@ -36,8 +38,7 @@ co = cohere.ClientV2("COHERE_API_KEY") # Get your free API key here: https://das import cohere co = cohere.ClientV2( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` @@ -112,10 +113,7 @@ query = "Are there fitness-related perks?" # Rerank the documents results = co.rerank( - model="rerank-v3.5", - query=query, - documents=documents, - top_n=2 + model="rerank-v3.5", query=query, documents=documents, top_n=2 ) for result in results.results: @@ -130,10 +128,7 @@ query = "Are there fitness-related perks?" # Rerank the documents results = co.rerank( - model="rerank-v3.5", - query=query, - documents=documents, - top_n=2 + model="rerank-v3.5", query=query, documents=documents, top_n=2 ) for result in results.results: @@ -148,10 +143,7 @@ query = "Are there fitness-related perks?" # Rerank the documents results = co.rerank( - model="YOUR_MODEL_NAME", - query=query, - documents=documents, - top_n=2 + model="YOUR_MODEL_NAME", query=query, documents=documents, top_n=2 ) for result in results.results: @@ -169,7 +161,7 @@ results = co.rerank( model="YOUR_ENDPOINT_NAME", query=query, documents=documents, - top_n=2 + top_n=2, ) for result in results.results: @@ -184,10 +176,10 @@ query = "Are there fitness-related perks?" # Rerank the documents results = co.rerank( - model="model", # Pass a dummy string + model="model", # Pass a dummy string query=query, documents=documents, - top_n=2 + top_n=2, ) for result in results.results: diff --git a/fern/pages/v2/get-started/quickstart/sem-search-quickstart.mdx b/fern/pages/v2/get-started/quickstart/sem-search-quickstart.mdx index aaf4dd9a7..c4251a02a 100644 --- a/fern/pages/v2/get-started/quickstart/sem-search-quickstart.mdx +++ b/fern/pages/v2/get-started/quickstart/sem-search-quickstart.mdx @@ -28,7 +28,9 @@ Next, import the library and create a client. ```python PYTHON import cohere -co = cohere.ClientV2("COHERE_API_KEY") # Get your free API key here: https://dashboard.cohere.com/api-keys +co = cohere.ClientV2( + "COHERE_API_KEY" +) # Get your free API key here: https://dashboard.cohere.com/api-keys ``` @@ -38,8 +40,7 @@ co = cohere.ClientV2("COHERE_API_KEY") # Get your free API key here: https://das import cohere co = cohere.ClientV2( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` @@ -107,7 +108,7 @@ doc_emb = co.embed( model="embed-english-v3.0", input_type="search_document", texts=documents, - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -126,7 +127,7 @@ doc_emb = co.embed( model="embed-english-v3.0", input_type="search_document", texts=documents, - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -145,7 +146,7 @@ doc_emb = co.embed( model="YOUR_MODEL_NAME", input_type="search_document", texts=documents, - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -164,7 +165,7 @@ doc_emb = co.embed( model="YOUR_ENDPOINT_NAME", input_type="search_document", texts=documents, - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -182,7 +183,7 @@ documents = [ doc_emb = co.embed( input_type="search_document", texts=documents, - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -202,7 +203,7 @@ query_emb = co.embed( model="embed-english-v3.0", input_type="search_query", texts=[query], - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -217,7 +218,7 @@ query_emb = co.embed( model="embed-english-v3.0", input_type="search_query", texts=[query], - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -232,7 +233,7 @@ query_emb = co.embed( model="YOUR_MODEL_NAME", input_type="search_query", texts=[query], - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -247,7 +248,7 @@ query_emb = co.embed( model="YOUR_ENDPOINT_NAME", input_type="search_query", texts=[query], - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -261,7 +262,7 @@ query = "Ways to connect with my teammates" query_emb = co.embed( input_type="search_query", texts=[query], - embedding_types=["float"] + embedding_types=["float"], ).embeddings.float ``` @@ -274,17 +275,19 @@ Then, perform semantic search by computing the similarity between the query embe ```python PYTHON import numpy as np + # Compute dot product similarity and display results def return_results(query_emb, doc_emb, documents): - n = 2 # customize your top N results + n = 2 # customize your top N results scores = np.dot(query_emb, np.transpose(doc_emb))[0] max_idx = np.argsort(-scores)[:n] - + for rank, idx in enumerate(max_idx): print(f"Rank: {rank+1}") print(f"Score: {scores[idx]}") print(f"Document: {documents[idx]}\n") - + + return_results(query_emb, doc_emb, documents) ``` ```mdx wordWrap diff --git a/fern/pages/v2/get-started/quickstart/text-gen-quickstart.mdx b/fern/pages/v2/get-started/quickstart/text-gen-quickstart.mdx index 04202263f..557cb368e 100644 --- a/fern/pages/v2/get-started/quickstart/text-gen-quickstart.mdx +++ b/fern/pages/v2/get-started/quickstart/text-gen-quickstart.mdx @@ -26,7 +26,9 @@ Next, import the library and create a client. ```python PYTHON import cohere -co = cohere.ClientV2("COHERE_API_KEY") # Get your free API key here: https://dashboard.cohere.com/api-keys +co = cohere.ClientV2( + "COHERE_API_KEY" +) # Get your free API key here: https://dashboard.cohere.com/api-keys ``` @@ -36,8 +38,7 @@ co = cohere.ClientV2("COHERE_API_KEY") # Get your free API key here: https://das import cohere co = cohere.ClientV2( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` @@ -160,7 +161,7 @@ print(response.message.content[0].text) ```python PYTHON response = co.chat( - model="model", # Pass a dummy string + model="model", # Pass a dummy string messages=[ { "role": "user", @@ -188,21 +189,18 @@ response = co.chat( messages=[ { "role": "system", - "content": "You respond in concise sentences." - }, - { - "role": "user", - "content": "Hello" + "content": "You respond in concise sentences.", }, + {"role": "user", "content": "Hello"}, { "role": "assistant", - "content": "Hi, how can I help you today?" + "content": "Hi, how can I help you today?", }, { "role": "user", - "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates." - } - ] + "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.", + }, + ], ) print(response.message.content[0].text) @@ -216,21 +214,18 @@ response = co.chat( messages=[ { "role": "system", - "content": "You respond in concise sentences." - }, - { - "role": "user", - "content": "Hello" + "content": "You respond in concise sentences.", }, + {"role": "user", "content": "Hello"}, { "role": "assistant", - "content": "Hi, how can I help you today?" + "content": "Hi, how can I help you today?", }, { "role": "user", - "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates." - } - ] + "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.", + }, + ], ) print(response.message.content[0].text) @@ -244,21 +239,18 @@ response = co.chat( messages=[ { "role": "system", - "content": "You respond in concise sentences." - }, - { - "role": "user", - "content": "Hello" + "content": "You respond in concise sentences.", }, + {"role": "user", "content": "Hello"}, { "role": "assistant", - "content": "Hi, how can I help you today?" + "content": "Hi, how can I help you today?", }, { "role": "user", - "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates." - } - ] + "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.", + }, + ], ) ``` @@ -270,21 +262,18 @@ response = co.chat( messages=[ { "role": "system", - "content": "You respond in concise sentences." - }, - { - "role": "user", - "content": "Hello" + "content": "You respond in concise sentences.", }, + {"role": "user", "content": "Hello"}, { "role": "assistant", - "content": "Hi, how can I help you today?" + "content": "Hi, how can I help you today?", }, { "role": "user", - "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates." - } - ] + "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.", + }, + ], ) ``` @@ -292,25 +281,22 @@ response = co.chat( ```python PYTHON response = co.chat( - model="model", # Pass a dummy string + model="model", # Pass a dummy string messages=[ { "role": "system", - "content": "You respond in concise sentences." - }, - { - "role": "user", - "content": "Hello" + "content": "You respond in concise sentences.", }, + {"role": "user", "content": "Hello"}, { "role": "assistant", - "content": "Hi, how can I help you today?" + "content": "Hi, how can I help you today?", }, { "role": "user", - "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates." - } - ] + "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.", + }, + ], ) ``` @@ -334,7 +320,7 @@ res = co.chat_stream( "role": "user", "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.", } - ] + ], ) for chunk in res: @@ -353,7 +339,7 @@ res = co.chat_stream( "role": "user", "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.", } - ] + ], ) for chunk in res: @@ -372,7 +358,7 @@ res = co.chat_stream( "role": "user", "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.", } - ] + ], ) for chunk in res: @@ -391,7 +377,7 @@ res = co.chat_stream( "role": "user", "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.", } - ] + ], ) for chunk in res: @@ -404,13 +390,13 @@ for chunk in res: ```python PYTHON res = co.chat_stream( - model="model", # Pass a dummy string + model="model", # Pass a dummy string messages=[ { "role": "user", "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.", } - ] + ], ) for chunk in res: diff --git a/fern/pages/v2/get-started/quickstart/tool-use-quickstart.mdx b/fern/pages/v2/get-started/quickstart/tool-use-quickstart.mdx index 1b577e7f9..85a15e577 100644 --- a/fern/pages/v2/get-started/quickstart/tool-use-quickstart.mdx +++ b/fern/pages/v2/get-started/quickstart/tool-use-quickstart.mdx @@ -28,7 +28,9 @@ Next, import the library and create a client. ```python PYTHON import cohere -co = cohere.ClientV2("COHERE_API_KEY") # Get your free API key here: https://dashboard.cohere.com/api-keys +co = cohere.ClientV2( + "COHERE_API_KEY" +) # Get your free API key here: https://dashboard.cohere.com/api-keys ``` @@ -38,8 +40,7 @@ co = cohere.ClientV2("COHERE_API_KEY") # Get your free API key here: https://das import cohere co = cohere.ClientV2( - api_key="", # Leave this blank - base_url="" + api_key="", base_url="" # Leave this blank ) ``` @@ -98,29 +99,30 @@ We also need to define the tool schemas in a format that can be passed to the Ch ```python PYTHON def get_weather(location): - # Implement your tool calling logic here + # Implement your tool calling logic here return [{"temperature": "20C"}] # Return a list of objects e.g. [{"url": "abc.com", "text": "..."}, {"url": "xyz.com", "text": "..."}] + functions_map = {"get_weather": get_weather} tools = [ { - "type": "function", - "function": { + "type": "function", + "function": { "name": "get_weather", - "description" : "gets the weather of a given location", + "description": "gets the weather of a given location", "parameters": { "type": "object", "properties": { - "location": { - "type" : "string", - "description": "the location to get weather, example: San Fransisco, CA" - } + "location": { + "type": "string", + "description": "the location to get weather, example: San Fransisco, CA", + } }, - "required": ["location"] - } - } + "required": ["location"], + }, + }, }, ] ``` @@ -133,12 +135,12 @@ The LLM will then generate the tool calls (if any) and return the `tool_plan` an ```python PYTHON -messages = [{"role": "user", "content": "What's the weather in Toronto?"}] +messages = [ + {"role": "user", "content": "What's the weather in Toronto?"} +] response = co.chat( - model="command-r-plus-08-2024", - messages=messages, - tools=tools + model="command-r-plus-08-2024", messages=messages, tools=tools ) if response.message.tool_calls: @@ -155,12 +157,12 @@ if response.message.tool_calls: ```python PYTHON -messages = [{"role": "user", "content": "What's the weather in Toronto?"}] +messages = [ + {"role": "user", "content": "What's the weather in Toronto?"} +] response = co.chat( - model="command-r-plus-08-2024", - messages=messages, - tools=tools + model="command-r-plus-08-2024", messages=messages, tools=tools ) if response.message.tool_calls: @@ -177,12 +179,12 @@ if response.message.tool_calls: ```python PYTHON -messages = [{"role": "user", "content": "What's the weather in Toronto?"}] +messages = [ + {"role": "user", "content": "What's the weather in Toronto?"} +] response = co.chat( - model="YOUR_MODEL_NAME", - messages=messages, - tools=tools + model="YOUR_MODEL_NAME", messages=messages, tools=tools ) if response.message.tool_calls: @@ -199,12 +201,12 @@ if response.message.tool_calls: ```python PYTHON -messages = [{"role": "user", "content": "What's the weather in Toronto?"}] +messages = [ + {"role": "user", "content": "What's the weather in Toronto?"} +] response = co.chat( - model="YOUR_ENDPOINT_NAME", - messages=messages, - tools=tools + model="YOUR_ENDPOINT_NAME", messages=messages, tools=tools ) if response.message.tool_calls: @@ -221,12 +223,14 @@ if response.message.tool_calls: ```python PYTHON -messages = [{"role": "user", "content": "What's the weather in Toronto?"}] +messages = [ + {"role": "user", "content": "What's the weather in Toronto?"} +] response = co.chat( - model="model", # Pass a dummy string + model="model", # Pass a dummy string messages=messages, - tools=tools + tools=tools, ) if response.message.tool_calls: @@ -260,10 +264,19 @@ if response.message.tool_calls: ) tool_content = [] for data in tool_result: - tool_content.append({"type": "document", "document": {"data": json.dumps(data)}}) + tool_content.append( + { + "type": "document", + "document": {"data": json.dumps(data)}, + } + ) # Optional: add an "id" field in the "document" object, otherwise IDs are auto-generated messages.append( - {"role": "tool", "tool_call_id": tc.id, "content": tool_content} + { + "role": "tool", + "tool_call_id": tc.id, + "content": tool_content, + } ) ``` @@ -274,9 +287,7 @@ The results are passed back to the LLM, which generates the final response. ```python PYTHON response = co.chat( - model="command-r-plus-08-2024", - messages=messages, - tools=tools + model="command-r-plus-08-2024", messages=messages, tools=tools ) print(response.message.content[0].text) ``` @@ -285,9 +296,7 @@ print(response.message.content[0].text) ```python PYTHON response = co.chat( - model="command-r-plus-08-2024", - messages=messages, - tools=tools + model="command-r-plus-08-2024", messages=messages, tools=tools ) print(response.message.content[0].text) ``` @@ -296,9 +305,7 @@ print(response.message.content[0].text) ```python PYTHON response = co.chat( - model="YOUR_MODEL_NAME", - messages=messages, - tools=tools + model="YOUR_MODEL_NAME", messages=messages, tools=tools ) print(response.message.content[0].text) ``` @@ -307,9 +314,7 @@ print(response.message.content[0].text) ```python PYTHON response = co.chat( - model="YOUR_ENDPOINT_NAME", - messages=messages, - tools=tools + model="YOUR_ENDPOINT_NAME", messages=messages, tools=tools ) print(response.message.content[0].text) ``` @@ -318,9 +323,9 @@ print(response.message.content[0].text) ```python PYTHON response = co.chat( - model="model", # Pass a dummy string + model="model", # Pass a dummy string messages=messages, - tools=tools + tools=tools, ) print(response.message.content[0].text) ``` diff --git a/fern/pages/v2/tutorials/cohere-on-azure/azure-ai-rag.mdx b/fern/pages/v2/tutorials/cohere-on-azure/azure-ai-rag.mdx index ec0d22db3..9877e2c6d 100644 --- a/fern/pages/v2/tutorials/cohere-on-azure/azure-ai-rag.mdx +++ b/fern/pages/v2/tutorials/cohere-on-azure/azure-ai-rag.mdx @@ -385,7 +385,8 @@ Here is a sample conversation consisting of a few turns. ```python PYTHON -messages = run_chatbot("Hello, I have a question")``` +messages = run_chatbot("Hello, I have a question") +``` ```mdx RESPONSE: