MistralAI 代码手册之 Codestral¶
MistralAI 发布了 codestral-latest —— 一款代码生成模型。
Codestral 是 mistralai 推出的新一代代码模型,专为代码生成优化,精通 80 多种编程语言。该模型通过补全函数、编写测试用例和填充代码片段来简化开发任务,既能提升开发效率又可减少错误。Codestral 通过统一的 API 端点提供服务,是软件开发的多功能工具。
本手册将展示如何结合 llama-index 使用 codestral-latest
模型,指导您使用 Codestral 的中间填充(fill-in-the-middle)和指令(instruct)端点功能。
配置大语言模型¶
In [ ]:
Copied!
import os
os.environ["MISTRAL_API_KEY"] = "<YOUR MISTRAL API KEY>"
from llama_index.llms.mistralai import MistralAI
llm = MistralAI(model="codestral-latest", temperature=0.1)
import os
os.environ["MISTRAL_API_KEY"] = ""
from llama_index.llms.mistralai import MistralAI
llm = MistralAI(model="codestral-latest", temperature=0.1)
指令模式使用说明¶
编写斐波那契数列函数¶
In [ ]:
Copied!
from llama_index.core.llms import ChatMessage
messages = [ChatMessage(role="user", content="Write a function for fibonacci")]
response = llm.chat(messages)
print(response)
from llama_index.core.llms import ChatMessage
messages = [ChatMessage(role="user", content="Write a function for fibonacci")]
response = llm.chat(messages)
print(response)
assistant: Sure, here is a simple Python function that calculates the nth number in the Fibonacci sequence: ```python def fibonacci(n): if n <= 0: print("Input should be positive integer.") elif n == 1: return 0 elif n == 2: return 1 else: a, b = 0, 1 for i in range(2, n): a, b = b, a + b return b ``` You can use this function to find the nth number in the Fibonacci sequence by calling `fibonacci(n)`, where `n` is the position of the number you want to find. For example, `fibonacci(10)` will return the 10th number in the Fibonacci sequence.
编写使用LlamaIndex构建RAG管道的函数¶
注意:输出结果基本准确,但基于较旧版本的LlamaIndex包。
In [ ]:
Copied!
messages = [
ChatMessage(
role="user",
content="Write a function to build RAG pipeline using LlamaIndex.",
)
]
response = llm.chat(messages)
print(response)
messages = [
ChatMessage(
role="user",
content="Write a function to build RAG pipeline using LlamaIndex.",
)
]
response = llm.chat(messages)
print(response)
assistant: Sure, I can help you with that. Here's a basic example of how you can build a Retrieval Augmented Generation (RAG) pipeline using LlamaIndex. This example assumes that you have a list of documents. ```python from llama_index import VectorStoreIndex, SimpleDirectoryReader def build_rag_pipeline(documents_path): # Load documents documents = SimpleDirectoryReader(documents_path).load_data() # Create index index = VectorStoreIndex.from_documents(documents) # Create query engine query_engine = index.as_query_engine() return query_engine # Usage query_engine = build_rag_pipeline("path_to_your_documents") response = query_engine.query("Your query here") print(response) ``` In this code: 1. We first import the necessary classes from LlamaIndex. 2. We define a function `build_rag_pipeline` that takes a path to a directory of documents as input. 3. We load the documents using `SimpleDirectoryReader`. 4. We create an index from the documents using `VectorStoreIndex.from_documents`. 5. We create a query engine from the index using `index.as_query_engine`. 6. Finally, we return the query engine. You can use the query engine to ask questions about the documents. The query engine will use the index to retrieve relevant documents and then generate a response based on those documents.
填充中间代码¶
该功能允许用户通过提示词设置起始点,并可选地通过后缀和停止标记设定结束位置。随后 Codestral 模型会生成中间的代码片段,特别适合需要特定代码生成的任务。
在代码的起始和结束处填充代码。¶
In [ ]:
Copied!
prompt = "def multiply("
suffix = "return a*b"
response = llm.fill_in_middle(prompt, suffix)
print(
f"""
{prompt}
{response.text}
{suffix}
"""
)
prompt = "def multiply("
suffix = "return a*b"
response = llm.fill_in_middle(prompt, suffix)
print(
f"""
{prompt}
{response.text}
{suffix}
"""
)
def multiply( a, b): """ This function multiplies two numbers """ return a*b
在代码中填充起始、结束标记以及终止符¶
In [ ]:
Copied!
prompt = "def multiply(a,"
suffix = ""
stop = ["\n\n\n"]
response = llm.fill_in_middle(prompt, suffix, stop)
print(
f"""
{prompt}
{response.text}
{suffix}
"""
)
prompt = "def multiply(a,"
suffix = ""
stop = ["\n\n\n"]
response = llm.fill_in_middle(prompt, suffix, stop)
print(
f"""
{prompt}
{response.text}
{suffix}
"""
)
def multiply(a, b): return a * b # test the function print(multiply(2, 3)) # should print 6 print(multiply(-1, 5)) # should print -5 print(multiply(0, 99)) # should print 0 # we can also test the function with large numbers print(multiply(123456789, 987654321)) # should print 121932631132635269 # the function should also work with floating point numbers print(multiply(3.14, 2.71)) # should print approximately 8.5392 # the function should also work with negative floating point numbers print(multiply(-3.14, 2.71)) # should print approximately -8.5392 # the function should also work with mixed types (integer and floating point) print(multiply(2, 3.14)) # should print approximately 6.28