The most detailed tutorial on using Claude and accessing Claude-api in history

What is it?

Claude is a newly opened AI chat robot, one of the largest language models in the world, much more powerful than some previous models such as GPT-3, so Claude is considered to be the most powerful competitor of ChatGPT. Claude's research and development company is Anthropic, a startup focused on artificial intelligence safety and research, co-founded by former OpenAI employees. In March of this year, Anthropic received a $300 million investment from Google, and Google also received a 10% stake in it.

Demo written in two hours (including front and back ends): https://ai.w3school.top/claude/

why

According to the official introduction, Claude's core model is trained to become useful, honest and harmless. In addition, Claude can better understand and accept natural language, and it does not require complicated skills to talk to it, and you can easily get detailed and easy-to-understand answers.

Like large-scale language models such as ChatGPT, Claude has a wide range of application scenarios. It can easily complete tasks such as information search, content summary, writing assistance, idea generation, question answering, and programming. At present, Claude has been applied in many well-known products. For example, the knowledge note tool Notio AI uses Claude to assist users in intelligent writing. The foreign question-and-answer community Quora has also embedded Claude in its AI chat application Poe.

Important: Claude is free, at least for now

how to do it

At present, Claude has been embedded in Slack, a team collaboration and communication application, which is currently available for free. But our focus today is to teach you how to access Claude in your own applications.

Step 1: Sign up for Slack

Slack official website address: Click me to jump
insert image description here
to register as much as possible to choose to use Google's gmail mailbox, the success rate of follow-up operations is high. Do not use domestic mailboxes such as qq.

Step Two: Create a Workspace

The workspace is an independent collaborative environment, and each workspace has its own channels (Channels), members, permission settings, etc. Different workspaces are isolated from each other, and members and resources are not shared.

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
At this point, the workspace is created

Step 3: Add the Claude application to the workspace (this step requires magic)

Click on the link of Slack-Claude official website (please bring your own ladder).
You can also find it by the following operation:
insert image description here
insert image description here
insert image description here
insert image description here
Authorize to add Claude to Slack.
insert image description here
When this interface appears, please switch nodes by yourself, try to use global proxy, switch private browsing, etc.
The above interface appears, indicating that the current ip is blocked. Please switch nodes by yourself, try to use global proxy, switch private browsing and other methods.
Changed the browser, sure enough. Click Allow, it will be successful if Success appears
insert image description here

Step 4: Activate advanced functions

Go back to the workspace page, and the Claude application will automatically appear on the left. At this time, the chat will find that Claude will not reply to any messages.
Solution: Click Slack Connect on the left. If there is no such option, click Browse Slack, and find the Slack Connect
insert image description hereinsert image description here
insert image description here
community in the drop-down box. The experience of the small partners in the Slack Connect community told me that some accounts in this place do not have the opportunity to try it for free, but I have created a few It’s a workspace, and you can try it for free, probably because of the trial gmail mailbox. If you don’t have one, just re-register a new account, or try to create a new workspace and go through the process. It doesn’t matter if you have money. So when you register earlier, you will be asked to use your gmail mailbox as much as possible.
insert image description hereinsert image description here
Now the newly created channel will appear on the left.
insert image description here
Then we add the Claude app to this channel
insert image description here
insert image description here
. Enter the newly created channel and activate the advanced features.
insert image description here
Now you can chat with Claude happily.
insert image description here
Do you think this is the end? If you just want to experience Claude and use it to help you, that's really all there is to it.

Step 5: Access Api

So how to access it? We all know that Claude has not opened the API test yet, but there is still a way to access Claude.

0. Development

We use the cloud function provided by Laf to access the API, and the development speed is faster. It's 2023, if you don't know Laf yet, I can only say it again

Laf is a serverless framework that provides out-of-the-box cloud functions, cloud databases, object storage and other capabilities. It is a very clean and refreshing development platform. It is not only easy to get started, but also write code like blogging! life is short, you need laf:)
Address:
China: https://laf.run
Overseas version: https://laf.dev

Create a cloud function

insert image description here

Add dependency claude-api-slack

insert image description here

write the following code

This code is very simple, you can understand it by reading the comments. Except for the cloud function part, the main code is also applicable in ordinary nodejs

import cloud from '@lafjs/cloud'

// 云函数入口代码
// 接收一个question,和一个可选的上下文id:conversationId
export default async function (ctx: FunctionContext) {
    
    
  const {
    
     question, conversationId } = ctx.query
  return await askCluadeAPi(question, conversationId)
}
// 调用api的主要代码
async function askCluadeAPi(question, conversationId) {
    
    
  // 见以下第一个步骤,授权以及获取user-token 步骤
  const token = 'xoxp-xxxxxx'
  // 见以下第二个步骤,获取claude appid
  const bot = 'U0xxxxxx'
  // chatId就是工作区间里新建的渠道。channel名称
  const chatId = 'ai聊天'

  // 初始化claude
  const {
    
     Authenticator } = await import('claude-api-slack')

  // 通过缓存保存客户端,可以避免每次提问都是在新会话
  let claudeClient = cloud.shared.get('claudeClient')
  if (!claudeClient) {
    
    
    claudeClient = new Authenticator(token, bot)
    cloud.shared.set('claudeClient', claudeClient)
  }
  // 创建频道并返回房间ID:channel
  const channel = await claudeClient.newChannel(chatId)

  let result
  if (conversationId) {
    
    
    result = await claudeClient.sendMessage({
    
    
      text: question,
      channel,
      conversationId,
      onMessage: (originalMessage) => {
    
    
        console.log("loading", originalMessage)
      }
    })
  } else {
    
    
    result = await claudeClient.sendMessage({
    
    
      text: question,
      channel,
      onMessage: (originalMessage) => {
    
    
        // console.log("loading", originalMessage)
        console.log("loading", originalMessage)
      }
    })
  }
  console.log("success", result)
  return {
    
    
    code: 0,
    msg: result.text,
    conversationId: result.conversationId
  }
}

1. Authorization and obtaining user-token

  • Enter slack official website and log in
  • Enter the api configuration page and click me to jump
  • Click Your apps in the upper right corner of the page
  • Click Create an App
  • ClickFrom scratch
    insert image description here
  • Enter the App Name, select the workspace created earlier, and click Create App
    insert image description here
  • Click OAuth & Permissions on the left sidebar
  • Find the User Token Scopes under the Scopes module, click the Add an OAuth Scopes button, search and add the following permissions in turn
channels:history
channels:read
channels:write
groups:history
groups:read
groups:write
chat:write
im:history
im:write
mpim:history
mpim:write

insert image description here
Note: The search here is a fuzzy search, don't make a mistake when selecting permissions

  • Click the Install to Workspace button under OAuth Tokens for Your Workspace to confirm the authorization.
    insert image description here
    insert image description here
    At this point, you have a series of User OAuth Tokens, find OAuth Tokens for Your Workspace
    insert image description here

2. Obtain claude appid

insert image description here

Step Six: Testinsert image description here

Join the contextual dialogue
insert image description here
Click publish in the upper right corner, copy the link on the left, and call it anywhere on the front end!

write at the end

Here is the demo address again https://ai.w3school.top/claude/

If you also want to join Claude, you can click this link to sign up, communicate with classmates in the community, and have a chance to win rich prizes.

Guess you like

Origin blog.csdn.net/xinTianou123/article/details/130938247